1 <dependencies> 2 <dependency> 3 <groupId>mysql</groupId> 4 <artifactId>mysql-connector-java</artifactId> 5 <version>8.0.18</version> 6 </dependency> 7 </dependencies>
1 import java.sql.Connection; 2 import java.sql.DriverManager; 3 import java.sql.PreparedStatement; 4 import java.sql.SQLException; 5 6 public class LinkDatabaseInsert { 7 public static void main(String[] args) throws ClassNotFoundException, SQLException { 8 //1.註冊數據庫的驅動 9 Class.forName("com.mysql.jdbc.Driver"); 10 //2.獲取數據庫鏈接(裏面內容依次是:"jdbc:mysql://主機名:端口號/數據庫名","用戶名","登陸密碼") 11 Connection connection = DriverManager.getConnection("jdbc:mysql://rm-uf6lgkv4fd9776rxego.mysql.rds.aliyuncs.com:3306/study","root","whmilyY123!"); 12 //3.須要執行的sql語句(?是佔位符,表明一個參數) 13 String sql = "insert into stu(id,name,age) values(?,?,?)"; 14 //4.獲取預處理對象,並依次給參數賦值 15 PreparedStatement statement = connection.prepareCall(sql); 16 statement.setInt(1,12); //數據庫字段類型是int,就是setInt;1表明第一個參數 17 statement.setString(2,"小明"); //數據庫字段類型是String,就是setString;2表明第二個參數 18 statement.setInt(3,16); //數據庫字段類型是int,就是setInt;3表明第三個參數 19 //5.執行sql語句(執行了幾條記錄,就返回幾) 20 int i = statement.executeUpdate(); 21 System.out.println(i); 22 //6.關閉jdbc鏈接 23 statement.close(); 24 connection.close(); 25 } 26 }
1 driver=com.mysql.jdbc.Driver 2 url=jdbc:mysql://rm-uf6lgkv4fd9776rxego.mysql.rds.aliyuncs.com:3306/study 3 user=root 4 password=whmilyY123!
1 package util; 2 3 import java.io.InputStream; 4 import java.sql.Connection; 5 import java.sql.DriverManager; 6 import java.util.Properties; 7 8 //獲取到db.properties文件中的數據庫信息 9 public class JdbcUtil { 10 //私有變量 11 private static String driver; 12 private static String url; 13 private static String user; 14 private static String password; 15 16 //靜態塊 17 static{ 18 try{ 19 //1.新建屬性集對象 20 Properties properties = new Properties(); 21 //2經過反射,新建字符輸入流,讀取db.properties文件 22 InputStream input = JdbcUtil.class.getClassLoader().getResourceAsStream("db.properties"); 23 //3.將輸入流中讀取到的屬性,加載到properties屬性集對象中 24 properties.load(input); 25 //4.根據鍵,獲取properties中對應的值 26 driver = properties.getProperty("driver"); 27 url = properties.getProperty("url"); 28 user = properties.getProperty("user"); 29 password = properties.getProperty("password"); 30 }catch(Exception e){ 31 e.printStackTrace(); 32 } 33 } 34 35 //返回數據庫鏈接 36 public static Connection getConnection(){ 37 try{ 38 //註冊數據庫的驅動 39 Class.forName(driver); 40 //獲取數據庫鏈接(裏面內容依次是:主機名和端口、用戶名、密碼) 41 Connection connection = DriverManager.getConnection(url,user,password); 42 //返回數據庫鏈接 43 return connection; 44 }catch (Exception e){ 45 e.printStackTrace(); 46 } 47 return null; 48 } 49 }
1 import util.JdbcUtil; 2 3 import java.sql.Connection; 4 import java.sql.PreparedStatement; 5 import java.sql.SQLException; 6 7 public class LinkMysql { 8 public static void main(String[] args) throws ClassNotFoundException, SQLException { 9 //獲取數據庫鏈接 10 Connection connection = JdbcUtil.getConnection(); 11 //須要執行的sql語句 12 String sql = "insert into stu(id,name,age) values(?,?,?)"; 13 //獲取預處理對象,並給參數賦值 14 PreparedStatement statement = connection.prepareCall(sql); 15 statement.setInt(1,14); 16 statement.setString(2,"李四"); 17 statement.setInt(3,16); 18 //執行sql語句(插入了幾條記錄,就返回幾) 19 int i = statement.executeUpdate(); //executeUpdate:執行並更新 20 System.out.println(i); 21 //關閉jdbc鏈接 22 statement.close(); 23 connection.close(); 24 } 25 }
1 package util; 2 3 import java.sql.*; 4 import java.util.ResourceBundle; 5 6 public class JdbcUtil2 { 7 //私有變量 8 private static String driver; 9 private static String url; 10 private static String user; 11 private static String password; 12 13 //靜態塊 14 static{ 15 try{ 16 //2.3經過ResourceBundle類拿到數據庫鏈接信息 17 ResourceBundle resourceBundle = ResourceBundle.getBundle("db"); 18 driver = resourceBundle.getString("driver"); 19 url = resourceBundle.getString("url"); 20 user = resourceBundle.getString("user"); 21 password = resourceBundle.getString("password"); 22 }catch(Exception e){ 23 e.printStackTrace(); 24 } 25 } 26 27 //返回數據庫鏈接 28 public static Connection getConnection(){ 29 try{ 30 //註冊數據庫的驅動 31 Class.forName(driver); 32 //獲取數據庫鏈接(裏面內容依次是:主機名和端口、用戶名、密碼) 33 Connection connection = DriverManager.getConnection(url,user,password); 34 //返回數據庫鏈接 35 return connection; 36 }catch (Exception e){ 37 e.printStackTrace(); 38 } 39 return null; 40 } 41 42 //關閉結果集 43 public static void closeResultSet(ResultSet resultSet) { 44 if (resultSet != null) { 45 try { 46 resultSet.close(); 47 } catch (SQLException e) { 48 e.printStackTrace(); 49 } 50 } 51 } 52 53 //關閉預處理對象 54 public static void closeStatement(Statement statement) { 55 if (statement != null) { 56 try { 57 statement.close(); 58 } catch (SQLException e) { 59 e.printStackTrace(); 60 } 61 } 62 } 63 64 //關閉數據庫鏈接 65 public static void closeConnection(Connection connection){ 66 if(connection != null){ 67 try { 68 connection.close(); 69 } catch (SQLException e) { 70 e.printStackTrace(); 71 } 72 } 73 } 74 75 //一次性關閉上面三個 76 public static void closeResource(ResultSet resultSet,Statement statement,Connection connection){ 77 closeResultSet(resultSet); 78 closeStatement(statement); 79 closeConnection(connection); 80 } 81 }
1 import util.JdbcUtil2; 2 3 import java.sql.Connection; 4 import java.sql.PreparedStatement; 5 import java.sql.SQLException; 6 7 public class LinkMysql2 { 8 public static void main(String[] args) throws ClassNotFoundException, SQLException { 9 //獲取數據庫鏈接 10 Connection connection = JdbcUtil2.getConnection(); 11 //須要執行的sql語句 12 String sql = "insert into stu(id,name,age) values(?,?,?)"; 13 //獲取預處理對象,並給參數賦值 14 PreparedStatement statement = connection.prepareCall(sql); 15 statement.setInt(1,19); 16 statement.setString(2,"王五"); 17 statement.setInt(3,16); 18 //執行sql語句(執行了幾條記錄,就返回幾) 19 int i = statement.executeUpdate(); //executeUpdate:執行並更新 20 System.out.println(i); 21 //關閉jdbc鏈接 22 JdbcUtil2.closeResource(null,statement,connection); 23 } 24 }