JDBC
java database connection(java数据库连接),一套数据库的API
使用前提:
导包:mysql-connector-java.jar
方法一:
public static viod main(String[] args)throws Exception{
//1、注册驱动
Class.forName("com.mysql.jdbc.Driver");
//2、获取数据库连接
//DriverManager.getConnection(String url,String user,String password)
//url:数据库地址
//user:账号
//password:密码
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db1,"root","root");
//3、获取执行sql的API:Statement/PreparedStatement
/*3.1获取Statement
更新操作(insert/update/delete)
int executeUpdate(String sql)
查询(select)
ResultSet executeQuery(String sql)
*/
Statement statement = conn.createStatement();
//3.2写sql
String sql = "insert into t_student(xh,name,sex,clsid) values(110,'小明','男',1)";
//3.3执行sql
statement.executeUpdate(sql);
//4、关闭资源
statement.close();
conn.close();
}
方法二:(推荐)
/* 获取preparedStatement
PreparedStatement prepareStatement(String sql):执行预编译sql(执行带占位符的sql)
预编译sql:
sql中的参数使用占位符(?)代替
PreparedStatement设置参数:
提供了很多的set方法用来设置参数:(根据参数的类型来选择对应的set方法)
参数 方法
int setInt(int parameterIndex,Int x)
double setDouble(int parameterIndex, double x)
String setString(int paramterIndex,String x)
...
parameterIndex:第几个占位符设置参数
x:设置的数据
更新操作(insert/update/delete):
int ecuteUpdate(String sql)
查询操作
ResultSet executeQuery(String sql)*/
public static void addStudent(){
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db1,"root","root");
//1、写sql
String sql = "insert into t_student(xh,name,sex,clsid) values(?,?,?,?)";
//2、获取PreparedStatement
PreparedStatement preparedStatement = conn.PrepareStatement(sql);
//3、设置参数
preparedStatement.setString(1,student.getXh()); preparedStatement.setString(1,student.getName());
preparedStatement.setString(1,student.getSex()); preparedStatement.setInt(1,student.getClsid());
//4、执行sql
preparedStatement.executeUpdate();
//注意这里的preparedStatement上面已经设置过了,所以这里不能带参数,如果带参会被认为这里是不使用标识符的sql语句
//关闭资源
statement.close();
conn.close();
}
ResultSet()方法进行查询
/*boolean next() --->判断还有没有数据
提供了很多的get方法,获取不同类型的数据
数据类型 方法
String String getString(int columnIndex)/String getInt(String columnLabel)
Int int getInt(int columnIndex)/int getInt(String columnLabel)
...
int columIndex:字段位置
the first column is 1, the second is 2,...
*/
//查询多个数据
public static void findAll() throws Exception{
//1、注册驱动
Class.forName("com.mysql.jdbc.Driver");
//2、连接数据库
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db1,"root","root");
//3、获取执行sql的对象
Statement statement = conn.createStatement();
String sql = "select * from t_student";
//4、执行sql
ResultSet rs = statment.executeQuery(sql);
//5、解析查询结果,封装到对象中
while(rs.next()){
//有数据,解析
//获取每个字段值
int id2 = rs.getInt("id");
String xh = rs.getString("xh");
String name = rs.getString("name");
String sex = rs.getString("sex");
int clsid = rs.getInt("clsid");
//封装到Student对象
Student student = new Student(id,xh,name,sex,clsid);
}
}
注意查询结果有多个时,需要用循环来让指针不停的向下移动
循环条件:
rs.next()—>代表当前行有没有数据
true:当前有数据,进入while循环,解析和封装数据
flase:当前没有数据,结束循环
JDBC代码优化
创建工具类
public class JDBCutils(){
//一般都是静态方法
//获取数据库连接
public static Connection getConnection(){
try{
//1、注册驱动
Class.forName("com.mysql.jdbc.Driver");
//2、获取数据库连接
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/db1,"root","root");
}catch(Exception e){
e.printStackTrace();
}
}return connection;
}
//关闭资源
public static void release(Connection connection, Statement statement, ResultSet resultSet){
if(connection!=null){
try {
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(statement!=null){
try {
statement.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(resultSet!=null){
try {
resultSet.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}