80_Java_JDBC1_Driver_PreparedStatement
【摘要】 JDBC
Driver
prepareStatement
JDBC(Java Database Connectivity)
是一个独立于特定数据库管理系统, 通用的SQL数据库存取和操作的公共接口(一组API),定义了用来访问数据库的标准Java类库,
(java.sql,javax.sql)使用这个类库可以以一种标准的方法、方便地访问数据库资源
JDBC接口(API)包括两个层次:
面向应用的API:Java API,抽象接口,供应用程序开发人员使用(连接数据库,执行SQL语句,获得结果)。
面向数据库的API:Java Driver API,供开发商开发数据库驱动程序
JDBC API 是一系列的接口,它使得应用程序能够进行数据库联接,执行 SQL语句,并且得到返回结果
Driver 接口
1 java.sql.Driver 接口是所有JDBC驱动程序需要实现的接口,接口是提供给数据库厂商使用的,不同数据库厂商提供不同的实现
2 驱动程序管理器类(java.sql.DriverManager)去调用这些Driver实现,例:
Oracle的驱动:oracle.jdbc.driver.OracleDriver
mySql的驱动: com.mysql.jdbc.Driver
加载与注册 JDBC驱动
1 调用 Class 类的静态方法 forName(),向其传递要加载的JDBC驱动的类名 ; Class.forName(“com.mysql.jdbc.Driver”);
2 DriverManager 类是驱动程序管理器类,负责管理驱动程序 ;DriverManager.registerDriver(com.mysql.jdbc.Driver);(不显示调用,内部静态代码块实现)
建立连接(Connection)
调用 DriverManager 类的 getConnection() 方法建立到数据库的连接
Connection conn = DriverManager.getConnection(url, user, password);
String url = "jdbc:mysql://localhost:3306/test";
1 User,password可以用“属性名=属性值”
2 JDBC URL 用于标识一个被注册的驱动程序 —— jdbc:子协议:子名称
协议:JDBC URL中的协议总是jdbc
子协议:子协议用于标识一个数据库驱动程序
子名称:一种标识数据库的方法,包含主机名(对应服务端的ip地址),端口号,数据库名
public class ConnectionTest {
// 方式一:
@Test
public void test1() throws SQLException {
// 获取Driver实现类对象
Driver driver = new com.mysql.jdbc.Driver();
// url:http://localhost:8080/gmall/keyboard.jpg
// jdbc:mysql:协议
// localhost:ip地址
// 3306:默认mysql的端口号
// test:test数据库
String url = "jdbc:mysql://localhost:3306/test";
// 将用户名和密码封装在Properties中
Properties info = new Properties();
info.setProperty("user","root");
info.setProperty("password","123456");
Connection connect = driver.connect(url, info);
System.out.println(connect); //com.mysql.jdbc.JDBC4Connection@4ca8195f
}
// 方式二
@Test
public void test2() throws Exception {
// 不希望有第三方的api 通过new com.mysql.jdbc.Driver()实现
//1.读取配置文件中的4个基本信息
InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc1.properties");
Properties properties = new Properties();
properties.load(is);
String user = properties.getProperty("user");
String password = properties.getProperty("password");
String url = properties.getProperty("url");
String driver = properties.getProperty("driverClass");
//2.加载驱动
Class.forName(driver);
//3.获取连接
Connection connection = DriverManager.getConnection(url, user, password);
System.out.println(connection); //com.mysql.jdbc.JDBC4Connection@44e81672
}}
//jdbc1.properties src 下面
user=root
password=123456
url=jdbc:mysql://localhost:3306/test?useSSL=false
driverClass=com.mysql.jdbc.Driver
PreparedStatement
1 可以通过调用 Connection 对象的 preparedStatement() 方法获取 PreparedStatement 对象
2 PreparedStatement 接口是 Statement 的子接口,它表示一条预编译过的 SQL 语句
3 PreparedStatement 对象所代表的SQL 语句中的参数用问号(?)来表示,
调用 PreparedStatement对象的 setXxx()方法来设置这些参数; setXxx()方法有两个参数,
第一个参数是要设置的 SQL 语句中的参数的索引(从1开始)
第二个是设置的 SQL 语句中的参数的值
@Test
public void test3() throws IOException, ClassNotFoundException, SQLException {
InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc1.properties");
Properties properties = new Properties();
properties.load(is);
String user = properties.getProperty("user");
String password = properties.getProperty("password");
String url = properties.getProperty("url");
String driver = properties.getProperty("driverClass");
//2.加载驱动
Class.forName(driver);
//3.获取连接
Connection connection = DriverManager.getConnection(url, user, password);
//4.预编译sql语句,返回PreparedStatement的实例
String sql = "update customers set name = ? where id = ?";
PreparedStatement ps = connection.prepareStatement(sql);
//5.填充占位符
ps.setObject(1, "alex");
ps.setObject(2, 2);
//6.执行
ps.execute();
// 7 关闭资源
ps.close();
connection.close();
}
普通查询
@Test
public void testQuery1(){
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = JDBCUtils.getConnection();
String sql = "select order_id,order_name,order_date from `order` where order_id = ?";
ps = conn.prepareStatement(sql);
ps.setObject(1, 1);
rs = ps.executeQuery();
if(rs.next()){
int id = (int) rs.getObject(1);
String name = (String) rs.getObject(2);
Date date = (Date) rs.getObject(3);
Order order = new Order(id, name, date);
System.out.println(order);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
JDBCUtils.closeResource(conn, ps, rs);
}
}
【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
作者其他文章
评论(0)