Java JDBC篇1——初识JDBC
生活随笔
收集整理的這篇文章主要介紹了
Java JDBC篇1——初识JDBC
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Java JDBC篇1——初識JDBC
Java DataBase Connectivity Java 數據庫連接(Java語言操作數據庫)
1、什么是JDBC
其實是官方定義的一套操作所有關系型數據庫的規則(接口),各個數據庫廠商去實現這套接口,提供數據庫驅動jar包,我們使用這套接口(JDBC)編程,真正執行的代碼是驅動jar包中的實現類
2、MySql驅動包
官網地址 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、建表和數據
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、注冊驅動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、執行sqlresultSet = statement.executeQuery(sql);//6、取出結果while (resultSet.next()){System.out.println(resultSet.getString("username"));}} catch (ClassNotFoundException e) {e.printStackTrace();} catch (SQLException throwables) {throwables.printStackTrace();} finally {//7、關閉連接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、注冊驅動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、執行sqlresultSet = statement.executeQuery(sql);//6、取出結果while (resultSet.next()){System.out.println(resultSet.getString("username"));}} catch (ClassNotFoundException e) {e.printStackTrace();} catch (SQLException throwables) {throwables.printStackTrace();} finally {//7、關閉連接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”);
查看源碼發現:在com.mysql.cj.jdbc.Driver類中存在靜態代碼塊
static {try {java.sql.DriverManager.registerDriver(new Driver());} catch (SQLException E) {throw new RuntimeException("Can't register driver!");} }mysql5之后的驅動jar包可以省略注冊驅動的步驟
5.x
Class.forName("com.mysql.jdbc.Driver");8.x
Class.forName("com.mysql.cj.jdbc.Driver");4.2、DriverManager(數據庫連接對象)
| Connection getConnection(String url, String user, String password) | 通過連接字符串和用戶名,密碼來獲取數據庫連接對象 |
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(數據庫連接對象)
| Statement createStatement() | 創建 SQL語句執行對象 |
| PreparedStatement prepareStatement(String sql) | 創建 SQL語句執行對象(防注入) |
4.4、Statement(執行sql對象)
| boolean execute(String sql) | 可以執行任意的sql語句 |
| int executeUpdate(String sql) | 執行DML(insert、update、delete)DDL(create,alter、drop)返回值:影響的行數,可以通過這個影響的行數判斷DML語句是否執行成功 |
| ResultSet executeQuery(String sql) | 執行DQL(select)語句 |
4.5、ResultSet(結果集對象)
| boolean next() | 游標向下移動一行,判斷當前行是否是最后一行末尾(是否有數據),如果是,則返回false,如果不是則返回true |
| Xxx getXxx(參數): | 獲取數據,Xxx:代表數據類型(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);} }總結
以上是生活随笔為你收集整理的Java JDBC篇1——初识JDBC的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MongoDB解决“Error pars
- 下一篇: 图的存储结构matlab,matlab存