标签:mysql etc resources load return use cti url round
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-uf6lgego.mysql.rds.aliyuncs.com:3306/study","root","wY123!"); 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-uf6lg6rxego.mysql.rds.aliyuncs.com:3306/study 3 user=root 4 password=wY123!
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 }
IDEA用Maven连接MySQL的jdbc驱动,并操作数据库
标签:mysql etc resources load return use cti url round
原文地址:https://www.cnblogs.com/dadian/p/11938707.html