Mysql第六章JDBC连接MySQL数据库
JDBC概念
Java数据库连接技术(Java Database Connectivity),能实现Java程序对各种数据库的访问。它由一组使用Java语言编写的类和接口(JDBC API)组成,它们位于java.sql以及javax.sql包中。
而每一种数据库的厂商都有对应该JDBC接口的实现类,生成一个jar包,通过jar包来连接java程序
JDBC访问数据库步骤
1.加载驱动
首先需要将mysql的jar包导入到项目中来,与项目关联上。然后通过Class.forName(com.mysql.jdbc.Driver)加载数据库厂商提供的驱动。每个数据库厂商提供的jar包不同。
2.获取Connection连接
加载完驱动后,可以通过DriverManager.getConnection(url,user,password);来获取connection连接对象
mysql的url为jdbc:mysql://IP地址:3306/库名,user为数据库的用户名,password为数据库的密码。
3.创建Statement或PreparedStatement执行SQL语句
通过Connection对象,可以创建Statement或PreparedStatement对象,来执行SQL语句操作数据库。
Statement接口
创建Statement对象的方法:
Statement stmt =connection.createStatement()
PreparedStatement接口
是一种预编译执行sql语句的方式(先把sql语句发给DBMS检查)
PreparedStatement接口继承Statement接口
创建PreparedStatement对象方法:
PreparedStatement pstm =connection.prepareStatement(sql语句)
sql语句中可以使用"?"作为数据占位符
使用SetXxx()方法或者SetObject()方法设置数据
SetObject()方法中第一个参数为要填充的问号的序号,从1开始;第二个参数是该问号位置需要填充的数值
对比Statement优势:
1.效率、性能、开销
2.安全性(防止sql注入)
3.代码可读性
4.返回ResultSet结果集
(1)查询操作返回结果集方法
查询操作返回结果集
statement.executeQuery(sql)
//或
preparedStatement.executeQuery()
sql为需要执行的sql语句,结果返回值为ResultSet类型,通过while(resultSet.next()){}来打印查询到的内容
(2)增删改操作返回结果集方法
增删改操作返回结果集的方法
statement.executeUpdate(sql)
//或
preparedStatement.executeUpdate()
sql为需要执行的sql语句,结果返回值为受影响行数,int类型,如果返回值大于0,代表操作成功
5.释放资源
关闭程序中运行的resultset、statement或preparedStatement、Connection等资源
查询操作示例:
import java.sql.*;
public class Homework2 {
public static void main(String[] args) {
Querystu(1002);
}
public static void Querystu(int id){
Connection conn = null;
PreparedStatement pstm = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("JDBC:mysql://localhost:3306/homework", "root", "111111");
pstm = conn.prepareStatement("SELECT studentno,studentname FROM student WHERE studentno =?");
pstm.setObject(1,id);
rs = pstm.executeQuery();
while(rs.next()) {
System.out.println(rs.getInt("studentno") + "\t" + rs.getString("studentname"));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
if (rs != null) {
rs.close();
}
if (pstm != null) {
pstm.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
增加数据操作示例:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class Homework3 {
public static void main(String[] args) {
add(6,"幼儿园");
}
public static void add(int id,String name){
Connection conn = null;
PreparedStatement psmt = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn =DriverManager.getConnection("JDBC:mysql://localhost:3306/homework", "root", "111111");
psmt = conn.prepareStatement("INSERT INTO grade VALUES (?,?)");
psmt.setObject(1,id);
psmt.setObject(2,name);
int i = psmt.executeUpdate();
if(i>0){
System.out.println("添加成功!");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
if(psmt!=null) {
psmt.close();
}
if(conn!=null){
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}