package com.bmi.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.bmi.bean.BmiBean;
public class DBHelper {
public static final String url="jdbc:mysql://127.0.0.1:3306/db_bmi"+"?useUnicode=true&characterEncodeing=utf8";
public static final String user="root";
public static final String password="wjljyx";
public static final String name="com.mysql.jdbc.Driver";
private Connection connection;
public PreparedStatement prepareStatement;
private static ResultSet set;
public DBHelper(String sql) {
//ctrl+1 快捷键
try {
Class.forName(name);
System.out.println("驱动加载成功!");
connection = DriverManager.getConnection(url, user, password);
System.out.println("驱动链接成功!");
prepareStatement = connection.prepareStatement(sql);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("驱动加载失败!");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("驱动链接失败!");
}
}
/*存储数据到数据库*/
public static boolean addBmi(BmiBean bean) {
String sql="insert tb_bmiInfo(date,height,weight,bmi) values(?,?,?,?)";
DBHelper db= new DBHelper(sql);
try {
db.prepareStatement.setString(1, bean.getDate());
db.prepareStatement.setString(2, bean.getHeight());
db.prepareStatement.setString(3, bean.getWeight());
db.prepareStatement.setString(4, bean.getBmi());
int update=db.prepareStatement.executeUpdate();
if (update>0) {
return true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
db.close();
}
return false;
}
/*
* */
public static List<BmiBean> getBmi() {
String sql="select * from tb_bmiInfo";
DBHelper db=new DBHelper(sql);
List<BmiBean> list=null;
try {
set = db.prepareStatement.executeQuery();
list= new ArrayList<>();
while(set.next()){
BmiBean bean=new BmiBean();
bean.setBmi(set.getString("bmi"));
bean.setDate(set.getString("date"));
bean.setHeight(set.getString("height"));
bean.setWeight(set.getString("weight"));
bean.setId(set.getInt("id"));
list.add(bean);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
db.close();
}
return list;
}
public static boolean delete_Bmi(String flag) {
int id=Integer.valueOf(flag);
String sql="delete from tb_bmiInfo where id=?";
DBHelper db= new DBHelper(sql);
System.out.println(sql);
try {
db.prepareStatement.setInt(1, id);
int update=db.prepareStatement.executeUpdate();
if (update>0) {
return true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
public void close(){
if(connection!=null){
try{
connection.close();
prepareStatement.close();
}catch(SQLException e){
e.printStackTrace();
}finally{
connection=null;
prepareStatement=null;
}
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
身体质量指数计算器(BMI)
共29个文件
jar:4个
java:4个
class:4个
需积分: 46 30 下载量 132 浏览量
2017-07-08
16:45:56
上传
评论 2
收藏 1.35MB RAR 举报
温馨提示
BMI身体质量指数计算器,包括前端和后台两部分,前端采用H5+js+ajax设计,后台Java设计(没有使用框架),界面比较美观,代码比较简洁。
资源推荐
资源详情
资源评论
收起资源包目录
BMI.rar (29个子文件)
新建文件夹
BMI
BMI
.settings
org.eclipse.wst.jsdt.ui.superType.name 6B
com.genuitec.eclipse.core.prefs 74B
org.eclipse.wst.common.project.facet.core.xml 414B
org.eclipse.wst.common.component 468B
org.eclipse.wst.jsdt.ui.superType.container 49B
.jsdtscope 522B
org.eclipse.jdt.core.prefs 364B
src
com
bmi
dao
DBHelper.java 3KB
CreatTable.java 544B
bean
BmiBean.java 721B
servlet
BmiServlet.java 3KB
.project 1KB
WebRoot
META-INF
MANIFEST.MF 39B
WEB-INF
web.xml 906B
classes
com
bmi
dao
CreatTable.class 1KB
DBHelper.class 4KB
bean
BmiBean.class 1KB
servlet
BmiServlet.class 3KB
lib
commons-fileupload-1.3.2.jar 69KB
mysql-connector-java-5.1.41-bin.jar 970KB
gson-2.1.jar 176KB
commons-io-2.4.jar 181KB
index.jsp 834B
.classpath 1KB
BMI(T)
css
style.css 2KB
img
index.html 2KB
js
bmi.js 239KB
style.js 3KB
.project 968B
共 29 条
- 1
资源评论
WangKingJ
- 粉丝: 45
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功