赞
踩
JDBC本质上属于一种服务,服务的特征,必须按照指定的规范进行操作
核心包java.sql
DriverManager
Connection
Statement
PreparedStatement
ResultSet
Mysql实例
private static final String JDBC_MYSQL_DRIVER = "com.mysql.cj.jdbc.Driver";
private static final String DB_MYSQL_URL =
"jdbc:mysql://localhost:3306/test?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC&characterEncoding=utf8";
static final String MYSQL_USER = "root";
static final String MYSQL_PASS = "root";
//注册驱动
Class.forName(JDBC_MYSQL_DRIVER);
//获取连接
Connection conn = DriverManager.getConnection(DB_MYSQL_URL,MYSQL_USER,MYSQL_PASS);
Oracle实例
private static final String JDBC_ORACLE_DRIVER = "oracle.jdbc.driver.OracleDriver";
private static final String DB_ORACLE_URL = "jdbc:oracle:thin:@10.254.254.137:1521:orcl";
static final String ORACLE_USER = "c##orcltest";static final String ORACLE_PASS = "orcltest";
工厂设计模式:
获取了java.sql.Connection接口对象之后,那么接下来就是执行SQL语句了,Statement接口就是SQL的执行器
public interface Statement extends Wrapper, AutoCloseable`
一条Sql的执行就是一个Statement对象
public Statement createStatement() throws SQLException
SQLException是JDBC数据库开发中的最顶级异常
public int executeUpdate(String sql) throws SQLException
public ResultSet executeQuery(String sql) throws SQLException
测试表:
CREATE TABLE `news` (
`nid` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(30) CHARACTER SET utf8 NOT NULL,
`read_count` int(11) DEFAULT NULL,
`price` double DEFAULT NULL,
`content` text CHARACTER SET utf8,
`pubdate` date DEFAULT NULL,
PRIMARY KEY (`nid`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8
String sql = "INSERT INTO news(title, read_count, price, content, pubdate) VALUES
('哈喽', 1, 20.0, '内容1', curdate())";
String sql2 = "UPDATE news SET title='UPDATE标题1',read_count=9999 where nid=6;";
int count = stmt.executeUpdate(sql2);
System.out.println("受影响的行数" + count);
String sql3 = "SELECT nid,title,read_count,price,content,pubdate FROM news;";
ResultSet rs = stmt.executeQuery(sql3);
while (rs.next()) {
int nid = rs.getInt(1);
String title = rs.getString(2);
int read = rs.getInt(3);
double price = rs.getDouble(4);
String content = rs.getString(5);
Date pubdate = rs.getDate(6);
System.out.println(nid + "\t" + title + "\t" + read + "\t" + price + "\t" + content + "\t" + pubdate);
}
PreparedStatement提供了SQL语句的预处理
public interface PreparedStatement extends Statement
public PreparedStatement prepareStatement(String sql) throws SQLException
获取对象的时候,就需要提供sql语句
--本次使用Oracle
DROP TABLE news PURGE;
DROP SEQUENCE news_seq;
CREATE SEQUENCE news_seq;
CREATE TABLE news(
nid NUMBER,
title VARCHAR2(30),
read_count INT,
price NUMBER,
content_news CLOB,
pubdate DATE,
CONSTRAINT pk_nid PRIMARY KEY(nid)
)
String title = "新闻标题"; int read = 99; double price = 99.8; String content = "这次测试的是Oracle"; java.util.Date pubdate = new java.util.Date(); String sql = "INSERT INTO news(nid, title, read_count, price, content_news, pubdate) VALUES " + " (news_seq.nextval, ?, ?, ?, ?, ?)"; //问号作为占位符 PreparedStatement perstmt = conn.prepareStatement(sql); //为占位符按照顺序设置内容 perstmt.setString(1, title); perstmt.setInt(2, read); perstmt.setDouble(3,price); perstmt.setString(4,content); System.out.println(pubdate.getTime()); perstmt.setDate(5,new java.sql.Date(pubdate.getTime())); int count = perstmt.executeUpdate(); System.out.println(count); conn.close();
String sql3 = "SELECT nid,title,read_count,price,content_news,pubdate FROM news
where nid=?";PreparedStatement perstmt = conn.prepareStatement(sql3);perstmt.setInt(1, 2);
ResultSet rs = perstmt.executeQuery();while (rs.next()) {
int nid = rs.getInt(1);
String title = rs.getString(2);
int read = rs.getInt(3);
double price = rs.getDouble(4);
String content = rs.getString(5);
Date pubdate = rs.getDate(6);
System.out.println(nid + "\t" + title + "\t" + read + "\t" + price + "\t" +
content + "\t" + pubdate);
}
如果对您有帮助,请点赞转发收藏,您的支持是我创作的最大动力
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。