java mysql 对数据的插入,更新

   最近在写一个小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后有空格

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值