Java JDBC篇1——初识JDBC
Java JDBC篇1——初識JDBC
Java DataBase Connectivity Java 數(shù)據(jù)庫連接(Java語言操作數(shù)據(jù)庫)
1、什么是JDBC
其實是官方定義的一套操作所有關(guān)系型數(shù)據(jù)庫的規(guī)則(接口),各個數(shù)據(jù)庫廠商去實現(xiàn)這套接口,提供數(shù)據(jù)庫驅(qū)動jar包,我們使用這套接口(JDBC)編程,真正執(zhí)行的代碼是驅(qū)動jar包中的實現(xiàn)類
2、MySql驅(qū)動包
官網(wǎng)地址 https://mvnrepository.com/artifact/mysql/mysql-connector-java
mysql-connector-java-5.1.49.jar 百度云:https://pan.baidu.com/s/17J2VfkGS2h44j69eB8TuFA提取碼:nhnt
mysql-connector-java-8.0.25.jar 百度云:https://pan.baidu.com/s/1b8n7650uMKJtwidoptOjNQ提取碼:wtvn
3、JDBC快速入門
3.1、建表和數(shù)據(jù)
USE test; CREATE TABLE USER (id INT PRIMARY KEY AUTO_INCREMENT ,username VARCHAR(50),PASSWORD VARCHAR(50),birthday DATE ); INSERT INTO USER (username, PASSWORD,birthday) VALUES('admin1', '123','2000-12-24'), ('admin2','123','2003-12-24'), ('test1', '123','2006-12-24'), ('test2', '123','2005-12-24');3.2、JDBC
5.x
public class Test {public static void main(String[] args) {Connection connection = null;Statement statement = null;ResultSet resultSet = null;try {// 1、注冊驅(qū)動Class.forName("com.mysql.jdbc.Driver");// 2、獲取連接String url="jdbc:mysql://localhost:3306/test";String username="root";String password="blingbling123.";connection = DriverManager.getConnection(url, username, password);//3、定義sqlString sql="select * from user";//4、獲取指定sql對象statement = connection.createStatement();//5、執(zhí)行sqlresultSet = statement.executeQuery(sql);//6、取出結(jié)果while (resultSet.next()){System.out.println(resultSet.getString("username"));}} catch (ClassNotFoundException e) {e.printStackTrace();} catch (SQLException throwables) {throwables.printStackTrace();} finally {//7、關(guān)閉連接if (resultSet!=null){try {resultSet.close();} catch (SQLException throwables) {throwables.printStackTrace();}}if (statement!=null){try {statement.close();} catch (SQLException throwables) {throwables.printStackTrace();}}if (connection!=null){try {connection.close();} catch (SQLException throwables) {throwables.printStackTrace();}}}} }8.x
public class Test {public static void main(String[] args) {Connection connection = null;Statement statement = null;ResultSet resultSet = null;try {// 1、注冊驅(qū)動Class.forName("com.mysql.cj.jdbc.Driver");// 2、獲取連接String url="jdbc:mysql://localhost:3306/test?serverTimezone=UTC&characterEncoding=utf-8&useSSL=false";String username="root";String password="blingbling123.";connection = DriverManager.getConnection(url, username, password);//3、定義sqlString sql="select * from user";//4、獲取指定sql對象statement = connection.createStatement();//5、執(zhí)行sqlresultSet = statement.executeQuery(sql);//6、取出結(jié)果while (resultSet.next()){System.out.println(resultSet.getString("username"));}} catch (ClassNotFoundException e) {e.printStackTrace();} catch (SQLException throwables) {throwables.printStackTrace();} finally {//7、關(guān)閉連接if (resultSet!=null){try {resultSet.close();} catch (SQLException throwables) {throwables.printStackTrace();}}if (statement!=null){try {statement.close();} catch (SQLException throwables) {throwables.printStackTrace();}}if (connection!=null){try {connection.close();} catch (SQLException throwables) {throwables.printStackTrace();}}}} }4、JDBC對象詳解
4.1、Class.forName(“com.mysql.jdbc.Driver”);
查看源碼發(fā)現(xiàn):在com.mysql.cj.jdbc.Driver類中存在靜態(tài)代碼塊
static {try {java.sql.DriverManager.registerDriver(new Driver());} catch (SQLException E) {throw new RuntimeException("Can't register driver!");} }mysql5之后的驅(qū)動jar包可以省略注冊驅(qū)動的步驟
5.x
Class.forName("com.mysql.jdbc.Driver");8.x
Class.forName("com.mysql.cj.jdbc.Driver");4.2、DriverManager(數(shù)據(jù)庫連接對象)
| Connection getConnection(String url, String user, String password) | 通過連接字符串和用戶名,密碼來獲取數(shù)據(jù)庫連接對象 |
5.x url
String url="jdbc:mysql://localhost:3306/test";8.x url
String url="jdbc:mysql://localhost:3306/test?serverTimezone=UTC&characterEncoding=utf-8&useSSL=false";4.3、Connection(數(shù)據(jù)庫連接對象)
| Statement createStatement() | 創(chuàng)建 SQL語句執(zhí)行對象 |
| PreparedStatement prepareStatement(String sql) | 創(chuàng)建 SQL語句執(zhí)行對象(防注入) |
4.4、Statement(執(zhí)行sql對象)
| boolean execute(String sql) | 可以執(zhí)行任意的sql語句 |
| int executeUpdate(String sql) | 執(zhí)行DML(insert、update、delete)DDL(create,alter、drop)返回值:影響的行數(shù),可以通過這個影響的行數(shù)判斷DML語句是否執(zhí)行成功 |
| ResultSet executeQuery(String sql) | 執(zhí)行DQL(select)語句 |
4.5、ResultSet(結(jié)果集對象)
| boolean next() | 游標向下移動一行,判斷當(dāng)前行是否是最后一行末尾(是否有數(shù)據(jù)),如果是,則返回false,如果不是則返回true |
| Xxx getXxx(參數(shù)): | 獲取數(shù)據(jù),Xxx:代表數(shù)據(jù)類型(int:列的編號,String:列名稱) |
5、抽取JDBC工具類
url=jdbc:mysql://localhost:3306/test user=root password=blingbling123. driver=com.mysql.jdbc.Driver public class JDBCtool {private static String urls;private static String user;private static String password;private static String driver;static {Properties properties=new Properties();ClassLoader classLoader=JDBCtool.class.getClassLoader();URL url=classLoader.getResource("connection.properties");String path=url.getPath();try {properties.load(new FileReader(path));} catch (IOException e) {e.printStackTrace();}urls=properties.getProperty("url");user=properties.getProperty("user");password=properties.getProperty("password");driver=properties.getProperty("driver");try {Class.forName(driver);} catch (ClassNotFoundException e) {e.printStackTrace();}}public static Connection getconnection() throws SQLException {return DriverManager.getConnection(urls,user,password);}public static void close(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet){if (resultSet!=null){try {resultSet.close();} catch (SQLException e) {e.printStackTrace();}}if (preparedStatement!=null){try {preparedStatement.close();} catch (SQLException e) {e.printStackTrace();}}if (connection!=null){try {connection.close();} catch (SQLException e) {e.printStackTrace();}}} } public class Test {public static void main(String[] args) throws SQLException {Connection connection = JDBCtool.getconnection();String sql="select * from user";PreparedStatement preparedStatement = connection.prepareStatement(sql);ResultSet resultSet = preparedStatement.executeQuery(sql);while (resultSet.next()){System.out.println(resultSet.getString("username"));}JDBCtool.close(connection,preparedStatement,resultSet);} }總結(jié)
以上是生活随笔為你收集整理的Java JDBC篇1——初识JDBC的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MongoDB解决“Error pars
- 下一篇: Java核心类库篇2——lang