最近在写一个小Bug,其中有对数据库表的操作,仅此记录下来,免得以后再入坑。(代码仅给自己看的,其他人如果不懂,那就不懂吧)
首先 连接数据库 在db包下新建 DBManager,获得一个con对象代码如下:
package db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBManager {
// 加载的驱动
private static final String DRIVER_CLASS = "com.mysql.jdbc.Driver";
// 数据库URL
private static final String DATABASE_URL = "jdbc:mysql://localhost:3306/db_test?useUnicode=true&characterEncoding=utf8";
// 数据库连接用户名
private static final String DATABASE_USERNAME = "root";
// 数据库连接密码
private static final String DATEBASE_PASSWORD = "root";
public static Connection getConnection() throws SQLException {
try {
Class.forName(DRIVER_CLASS);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Connection con = DriverManager.getConnection(DATABASE_URL,DATABASE_USERNAME, DATEBASE_PASSWORD);
return con;
}
}
然后就是对数据的增加,代码如下:
public static int register(String name, String bianhao, String password,String phone) {
int result = REGISTER_FAILED;
updateRowCnt = 0;
// 执行 SQL 插入语句
String sql ="insert into user_teacher(`name`, `bianhao`,`password`,`phone`) values ('"
+name+ "', '" +bianhao+ "', '" + password + "', '" + phone + "')";
try {
Connection con = DBManager.getConnection();
preparedStatement = con.prepareStatement(sql);
try{
updateRowCnt = preparedStatement.executeUpdate();
// 插入结果
if(updateRowCnt != 0){
result = REGISTER_SUCCEEDED;
}
preparedStatement.close();
con.close();
}catch(Exception e){
e.printStackTrace();
}
}catch(Exception e){
e.printStackTrace();
}
System.out.println("register service result:" + result);
return result;
}
最后就是更新,代码如下:
public static int startSignin(String time,String biaoming, int zhuangtai) {
int result = LOGIN_FAILED;
System.out.println("time biaoming zhuangtai " + time+" "+biaoming+" "+zhuangtai);
// 执行 SQL 查询语句
// String sql ="insert into user_teacher(`name`, `bianhao`,`password`,`phone`) values ('"
// +name+ "', '" +bianhao+ "', '" + password + "', '" + phone + "')";
// String sql="update db_book set name='"+book.getName()+"',
// price="+book.getPrice()+",author='"+book.getAuthor()+"',bookTypeId="+book.getBookTypeId()+" where id=13";
String sql ="update user_tas set `zhuangtai`='"+zhuangtai+"',`time`='"+time+"' where `biaoming`='"+biaoming+"' ";
try {
Connection con = DBManager.getConnection();
// preparedStatement = con.prepareStatement(sql);
try{
Statement stmt=(Statement) con.createStatement();//创建一个Statement连接
int flag=stmt.executeUpdate(sql);//执行sql语句
// 查询结果
System.out.println("update :" + result);
if(flag==1){
result=REGISTER_SUCCEEDED;
}
con.close();
}catch(Exception e){
e.printStackTrace();
}
}catch(Exception e){
e.printStackTrace();
}
System.out.println("login service result:" + result);
return result;
}
其中碰到过几个问题,也就几个细节 :
1.
就拿这段来说吧String sql ="insert into user_teacher(`name`, `bianhao`,`password`,`phone`) values ('"
+name+ "', '" +bianhao+ "', '" + password + "', '" + phone + "')";
`name` 不是 'name'
2.
当我们的表名不是写死的而是一个传过来的字符串时 可以用字符串拼接 例如
String sql= "select * from "+biaoming+" where name='" + name +"'";
但是要注意细节 譬如
String sql= "select * from"+biaoming+" where name='" + name +"'";
String sql= "select * from "+biaoming+"where name='" + name +"'";
String sql= "select * from "+biaoming+" where name='" + name +"'";
这三句是完全不一样的 前两句都会报错。
同理 当某一列也是传过来的字符串时 我们还是用字符串拼接。
String sql="update "+biaoming+" set "+ studentTime+"='1' where `name`='"+name+"' ";
这里表名:biaoming是字符串 列名studentTime也是传过来的字符串,当然同上,还是要注意细节。
String sql="update "+biaoming+" set "+ studentTime+"='1' where `name`='"+name+"' ";
update 后用空格,set前有空格,set后有空格