package daolmpl;
import java.sql.*;
public class joinDaoImpl {
//insert
public int insert(String name, int age, int score) {
Connection con = null;
PreparedStatement pst = null;
try {
//加载驱动
Class.forName("com.mysql.jdbc.Driver");//注册数据库的驱动
//获取数据库连接
con = DriverManager.getConnection("jdbc:mysql://localhost:3506/join?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8", "root", "123456");
con.setAutoCommit(false);
pst = (PreparedStatement) con.createStatement();
String sql = "insert into join-student values(?,?,?);";
pst.setString(1, name);
pst.setInt(2, age);
pst.setInt(3, score);
int i = pst.executeUpdate();
return i;
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
try {
con.rollback();
} catch (SQLException ex) {
ex.printStackTrace();
}
e.printStackTrace();
} finally {
try {
pst.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
return -1;
}
}
//update
public int update(int id, int score) {
Connection con = null;
PreparedStatement pst = null;
try {
//加载驱动
Class.forName("com.mysql.jdbc.Driver");//注册数据库的驱动
//获取数据库连接
con = DriverManager.getConnection("jdbc:mysql://localhost:3506/join?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8", "root", "123456");
con.setAutoCommit(false);
pst = (PreparedStatement) con.createStatement();
String sql = "update join-student set grade=? where id=?";
pst.setInt(1, id);
pst.setInt(2, score);
int i = pst.executeUpdate();
return i;
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
try {
con.rollback();
} catch (SQLException ex) {
ex.printStackTrace();
}
e.printStackTrace();
} finally {
try {
pst.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
return -1;
}
}
//select
public void select() {
Connection con = null;
PreparedStatement pst = null;
try {
//加载驱动
Class.forName("com.mysql.jdbc.Driver");//注册数据库的驱动
//获取数据库连接
con = DriverManager.getConnection("jdbc:mysql://localhost:3506/join?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8", "root", "123456");
con.setAutoCommit(false);
pst = (PreparedStatement) con.createStatement();
String sql = "select * from student";
//执行sql语句
ResultSet resultSet = pst.executeQuery();
while (resultSet.next()) {
//如果有下一条记录
String id = resultSet.getString("id");//根据列名返回值
String name = resultSet.getString("name");
int age = resultSet.getInt("age");
int score = resultSet.getInt("score");
System.out.println(id + "\t" + "name" + "\t" + age + "\t" + score);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
try {
con.rollback();
} catch (SQLException ex) {
ex.printStackTrace();
}
e.printStackTrace();
} finally {
try {
pst.close();
} catch (SQLException e) {
e.printStackTrace();
}
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
jdbc aumentar y cambiar el código semiacabado de verificación
Supongo que te gusta
Origin blog.csdn.net/weixin_46064382/article/details/106754284
Recomendado
Clasificación