JDBC程序编写步骤 :
- 注册驱动:告知JVM使用哪种数据库的驱动
- 获得连接:使用JDBC中的类,完成MySQL数据库连接
- 获得语句执行平台:面对连接对象获取对SQL语句的执行结果
- 执行SQL语句
- 处理结果
- 释放资源
获取数据库连接
使用properties 配置文件方式获得数据库连接方式。
在配置文件里写下四个基本信息。
- 然后通过系统类加载器获得配置文件properties的输入流。通过properties方法读取配置文件四个基本信息。
- 加载驱动
- 获得连接

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;
public class ConnectionMysql {
public static void getConnection() throws Exception {
//读取配置信息
InputStream ips = ConnectionMysql.class.getClassLoader().getResourceAsStream("jdbc.properties");
Properties pros = new Properties();
pros.load(ips);
String user = pros.getProperty("user");
String password = pros.getProperty("password");
String url = pros.getProperty("url");
String driver = pros.getProperty("driver");
//2.加载驱动
Class.forName(driver);
//3.获取连接
Connection con = DriverManager.getConnection(url,user,password);
System.out.println(con);
}
public static void main(String[] args) throws Exception {
ConnectionMysql.getConnection();
}
}
使用PreparedStatement 接口实现CRUD操作
PreparedStatement接口是Statement接口的子接口。Statement存在一些弊端,例如会被注入攻击等,我们不使用Statement接口。
PreparedStatement接口用来动态的执行SQL语句,因此在使用此接口前需要先写好SQL语句。
通过Connection接口中的preparedStatement(sql) 方法可以获得PreparedStatement的一个实例,参数列表里传入一个事先写好的sql语句,达到预编译的效果,传入的sql语句中用占位符占位。
然后使用PreparedStatement中的setObject方法填充占位符。
增删改:
PreparedStatement接口中的executeUpdate()方法,可以执行前面写好的包含参数的动态INSERT、UPDATE、DELETE语句。最后关闭资源即可。
查询:
查询与增删改不同,查询会从数据库中得到一个返回的结果集。
通过PreparedStatement接口中的executeQuery()方法可以执行SQL的查询,并返回查询生成的ResultSet对象。
ResultSet是一个结果集,类似于集合,使用next方法可以对结果集进行遍历的操作,并通过getInt(),getString()等方法获得具体数据表中值。
Blob类型数据
Blob类型数据是一个大容量的数据类型,如果要向数据表中插入图片等大容量的数据,就需要使用到Blob数据类型。
下面代码是一个java中向数据表中插入数据的代码。
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.Properties;
public class ConnectionMysql {
public static Connection getConnection() throws Exception {
//读取配置信息
InputStream ips = ConnectionMysql.class.getClassLoader().getResourceAsStream("jdbc.properties");
Properties pros = new Properties();
pros.load(ips);
String user = pros.getProperty("user");
String password = pros.getProperty("password");
String url = pros.getProperty("url");
String driver = pros.getProperty("driver");
//2.加载驱动
Class.forName(driver);
//3.获取连接
Connection con = DriverManager.getConnection(url,user,password);
return con;
}
public static void closeResource(Connection con,PreparedStatement ps) throws Exception{
con.close();
ps.close();
}
public static void insertBlob() throws Exception {
//建立数据库连接
Connection con = getConnection();
//预编译sql语句
String sql = "INSERT INTO users (uname,uemail,uphoto) VALUES (?,?,?)";
PreparedStatement ps = con.prepareStatement(sql);
//填充占位符
ps.setObject(1,"刘亦菲");
ps.setObject(2,"6666@163.com");
//添加照片这种大型数据需要使用Blob,要建立输入流
FileInputStream fis = new FileInputStream("E:\\刘亦菲.jpg");
ps.setBlob(3,fis);
ps.execute();
closeResource(con,ps);
}
public static void main(String[] args) throws Exception {
insertBlob();
}
}

8418

被折叠的 条评论
为什么被折叠?



