1. class.forname(数据库驱动名);
2.建立数据库连接;
使用DriverManager类的getConnection()静态方法来获取数据库连接对象,其语法格式如下所示:
Connection conn=DriverManager.getConnection(String url,String user,String pass);
其中url--数据库连接字符串.
user---数据库的用户名
pass---数据库密码
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import com.zfh.pojo.User;
public class JdbcConnection {
private static final String DRIVER = "com.mysql.jdbc.Driver";
private static final String URL = "jdbc:mysql://10.254.238.42:3306/jsp_servlet_jdbc";
private static final String USERNAME = "root";
private static final String PASSWORD = "Cuit12.@";
private static Connection conn = null;
private static PreparedStatement ps = null;
private static ResultSet rs = null;
static {
try {
Class.forName(DRIVER);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
3. 创建Statement对象;
对数据库进行操作或访问时,需要使用sql语句,在Java语言中,sql语句通过Statement对象进行封装,发送给数据库。Statement对象不是通过Statement类直接创建的,而是通过Connection对象所提供的方法来创建各种Statement对象。
Statement smt=Connection.createStatement();
4.执行sql语句;
public static int deleteUser(int id) throws SQLException {
conn = getConn();
String sql = "delete from t_user where id=?";
ps = conn.prepareStatement(sql);
ps.setInt(1, id);
return ps.executeUpdate();
}
public static int updateUser(User user) throws SQLException {
conn = getConn();
String sql = "update t_user set name=?,gender=?,age=?,score=?"
+ " where id=?";
ps = conn.prepareStatement(sql);
ps.setString(1, user.getName());
ps.setString(2, user.getGender());
ps.setInt(3, user.getAge());
ps.setDouble(4, user.getScore());
ps.setInt(5, user.getId());
return ps.executeUpdate();
}
public static int insertUser(User user) throws SQLException {
conn = getConn();
String sql = "insert into t_user(name,gender,age,score)"
+ " values(?,?,?,?)";
ps = conn.prepareStatement(sql);
ps.setString(1, user.getName());
ps.setString(2, user.getGender());
ps.setInt(3, user.getAge());
ps.setDouble(4, user.getScore());
return ps.executeUpdate();
}
public static void close(Connection myConn) throws SQLException {
if (myConn != null) {
myConn.close();
}
}
public static List selectUsers() throws SQLException {
List users = new ArrayList<>();
conn = getConn();
String sql = "select id,name,gender,age,score,create_time from t_user";
ps = conn.prepareStatement(sql);
rs= ps.executeQuery();
while (rs.next()) {
User user = new User();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
user.setGender(rs.getString("gender"));
user.setAge(rs.getInt("age"));
user.setScore(rs.getDouble("score"));
user.setCreate_time(rs.getDate("create_time"));
users.add(user);
}
return users;
}
public static User selectUser(int id) throws SQLException {
User user = null;
conn = getConn();
String sql = "select id,name,gender,age,score,create_time from t_user where id=?";
ps = conn.prepareStatement(sql);
ps.setInt(1, id);
rs= ps.executeQuery();
if (rs.next()) {
user = new User();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
user.setGender(rs.getString("gender"));
user.setAge(rs.getInt("age"));
user.setScore(rs.getDouble("score"));
user.setCreate_time(rs.getDate("create_time"));
}
return user;
}
public static Connection getConn() throws SQLException {
Connection conn = null;
conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
return conn;
}
} 


