JDBC的接口都在:java.sql包下
java.sql.DriverManager(类)
java.sql.Connection(数据库连接对象)
connection.close(); :关闭Java如数据库的链接
connection.createStatement();
prepareStatement()创建sql预处理对象
prepareCall()创建存储过程处理对象
java.sql.Statement(sql执行对象)
executeUpdate(String) 返回 int(所影响函数) 执行 insert update delete
executeQuery(String) ResultSet 执行 select 返回查询结果 结果就包装在 ResultSet(接口内)
批处理多条sql
addBatch(String) 添加 insert update delete
executeBatch() int[]
java.sql.PreparedStatement(SQL预处理)
java.sql.ResultSet(记录集对象)
java.sql.ResultSetMetaData(查询出来的表结构)
java.sql.CallableStatement(调用存储过程)
DataSource (数据库源 javax.sql)数据员:javax.sql.DataSource
DriverManager创建了Connection对象 Connection创建了接口
Statement接收Connection来实现sql语句
网页验证 ——逻辑运算——准备数据库申请sql———数据库
sql查询
ResultSet方法中的resultSet.next()方法查询
用while循环 因为while循环比较适合不知道次数的循环
查询:举例他有个类似光标一样的东西如果有就返回一个TRUE 没有就返回fales
如过有常用的代码你可以将它包装起来 节省工程量
jdbc链接:
public class Test {
private static Test ourInstance = new Test();
private DataSource dataSource = null;
public static Test getInstance() {
return ourInstance;
}
private Test() {
ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
try {
comboPooledDataSource.setDriverClass("com.mysql.cj.jdbc.Driver");
comboPooledDataSource.setJdbcUrl("");
comboPooledDataSource.setUser("root");
comboPooledDataSource.setPassword("");
comboPooledDataSource.setMaxPoolSize(3);
comboPooledDataSource.setMinPoolSize(1);
dataSource = comboPooledDataSource;
Connection connection = dataSource.getConnection();
System.out.println("初始化成功");
} catch (PropertyVetoException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
sql预处理
public static ResultSet getResultSet(Connection connection, String sql, Object... objects) throws SQLException {
PreparedStatement connection1 = connection.prepareStatement(sql);
for (int i = 0; i < objects.length; i++) {
connection1.setObject(i + 1, objects[i]);
}
ResultSet rs = connection1.executeQuery();
return rs;
}
public static int getUpdate(Connection connection, String sql, Object... objects) throws SQLException {
PreparedStatement pst = connection.prepareStatement(sql);
for (int i = 0; i < objects.length; i++) {
pst.setObject(i + 1, objects[i]);
}
int a = pst.executeUpdate();
return a;
}
sql事务:
public Connection getDataSource() throws SQLException {
Connection connection = dataSource.getConnection();
connection.getAutoCommit();
return connection;
}
public Connection getDataSource2() throws SQLException {
Connection connection = dataSource.getConnection();
connection.setAutoCommit(false);
return connection;
}
import java.sql.*;
import java.sql.Connection;
public class T1 {
public static <connection> void main(String[] args) throws Exception {
Connection connection = DriverManager.getConnection("jdbc:mysql://47.101.201.210/Test?useSSL=FALSE&serverTimezone=UTC",
"root",
"Sb1996350.");
Statement c = connection.createStatement();
//int a/*打印影响函数*/ = c.executeUpdate/*执行更新语句*/("");//删除 添加 更行 都是属于更新语句
// System.out.println(a);
Connection connection1 = T2.getInstance().getconnctin();//如果有经常要用的代码,可以将它封装起来、用的话直接通过方法调用就行
Statement statement = connection.createStatement();//这里将connection对象给 Statement
// 调用Statement 类中的sql执行代码
ResultSet resultSet = statement.executeQuery("select *from student");//jdbc一切以查询为主如果列起别名查询的时候就输别名
//resultSet 方法中每一个表的数据类型都有两种得值方式 一种是字符串类型(列的名字) 一种是整数型(第几行)
while (resultSet.next()) {
int sid=resultSet.getInt(1);//表的第一个个字段
System.out.println(sid);
String to=resultSet.getString(2);//表的第二个字段
System.out.println(to);
}
connection.close();//用完后及时关闭 良好的代码书写习惯
}
}