记录使用JDBC

记录使用JDBC 

1 mysql 加载驱动 实际上MYSQL 5以后会自动注册驱动 可以省略不写

2 通过驱动管理获取连接

3 使用PreparedStatement 执行SQL 语名防止注入

4 执行sql语句

5 获取结果 

package com.lanlin.web.dao;

import com.lanlin.web.domain.User;
import com.lanlin.web.util.GetConn;
import java.sql.*;
import com.mysql.jdbc.Driver;
/**
 * Created on 2019/3/6.0:07
 *
 * @author Lanlin
 */
public class UserDao {
    public UserDao() {
    }

    public static int save(String uname,String upwd)throws ClassNotFoundException, SQLException{
       Connection conn = null;
       PreparedStatement ps = null;
        conn= GetConn.getConnection();
        String sql="INSERT INTO user(username, userpwd) VALUES (?,?)";
        ps = conn.prepareStatement(sql);
        ps.setString(1,uname);
        ps.setString(2,upwd);
        ps.executeUpdate();
        int count = ps.getUpdateCount();
        return count;
    }

    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        int i = UserDao.save("mnnn1", "1532f");
        if(i>=1){
            System.out.println("添加成功");
        };
    }






}

  

猜你喜欢

转载自www.cnblogs.com/fslanlin/p/10480982.html