标签:获取 demo1 12px 操作 连接数量 exec 数据库 jar包 prepare
JDBC连接mysql数据库,执行更新sql指令的一般顺序如下(密码和sql语句略去)
1 package cn.gao.jdbc; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.Statement; 6 7 public class JdbcDemo1 { 8 public static void main(String[] args) throws Exception { 9 // 1,导入驱动jar包(mysql-connector-java-5.1.49-bin.jar),添加libs目录,拷贝jar到libs目录下 10 11 // 2,注册驱动(反射) 12 Class.forName("com.mysql.jdbc.Driver"); 13 // 3,获取数据库连接对象 Connection,不适用参数useSSL=false,会有警告,详看一下链接 14 // https://blog.csdn.net/qq_41785135/article/details/85118329 15 String url="jdbc:mysql://localhost:3306/mydata?useSSL=false"; 16 String username="root"; 17 String password="xxx"; 18 Connection conn = DriverManager.getConnection(url,username,password); 19 // 4,定义sql语句 20 String sql="update xxx set xxx=‘xxx‘ where xxx=xxx"; 21 // 5,获取执行sql语句的对象 Statement 22 Statement statement = conn.createStatement(); 23 // 6,执行sql,接收返回结果 24 int count = statement.executeUpdate(sql); 25 // 7,处理结果 26 System.out.println(count); 27 // 8,释放资源 28 statement.close(); 29 conn.close(); 30 31 } 32 }
执行Insert语句如下
1 package cn.gao.jdbc; 2 3 import java.sql.*; 4 5 /** 6 * 增加记录 statement.executeUpdate(sql); 7 */ 8 public class JdbcDemo2 { 9 public static void main(String[] args) { 10 Connection connection=null; 11 Statement statement=null; 12 try { 13 Class.forName("com.mysql.jdbc.Driver"); 14 String url="jdbc:mysql://localhost:3306/mydata?useSSL=false"; 15 String username="root"; 16 String password="xxx"; 17 connection = DriverManager.getConnection(url, username, password); 18 statement = connection.createStatement(); 19 String sql="INSERT INTO xxx VALUES(‘xxx‘,‘xxx‘,‘xxx。‘)"; 20 int count = statement.executeUpdate(sql); 21 if (count>=1){ 22 System.out.println("插入成功,受影响的行数:"+count); 23 } 24 else 25 { 26 System.out.println("插入失败"); 27 } 28 } catch (Exception e) { 29 e.printStackTrace(); 30 } 31 finally { 32 if(statement!=null){ 33 try { 34 statement.close(); 35 } catch (SQLException throwables) { 36 throwables.printStackTrace(); 37 } 38 } 39 if(connection!=null){ 40 try { 41 connection.close(); 42 } catch (SQLException throwables) { 43 throwables.printStackTrace(); 44 } 45 } 46 47 48 } 49 } 50 }
删除语句也类似,都是加载驱动,获取连接、定义sql,执行sql,关闭连接等等
这样就发现重复语句太多,且会遇到SQL注入问题(拼接sql过程中,通过输入逻辑语句造成某条语句发生逻辑变化),解决办法是执行sql是的对象使用PreparedStatement,sql字符串不直接拼接,通过传参的形式处理。
于是,将重复的字段抽取出来,封装成一个工具类(加载驱动、获取连接只需要创建一个工具类的对象即可)
1 package cn.gao.jdbc; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.SQLException; 6 import java.sql.Statement; 7 8 public class JdbcUtil { 9 Connection connection = null; 10 Statement statement = null; 11 12 /** 13 * 初始化方法 14 */ 15 public void init() { 16 try { 17 Class.forName("com.mysql.jdbc.Driver"); 18 String url = "jdbc:mysql://localhost:3306/mydata?useSSL=false"; 19 String username = "root"; 20 String password = "xxx"; 21 connection = DriverManager.getConnection(url, username, password); 22 statement = connection.createStatement(); 23 } catch (Exception e) { 24 e.printStackTrace(); 25 } 26 System.out.println("初始化成功"); 27 } 28 29 /** 30 * 关闭资源方法 31 */ 32 public void close() { 33 if (statement != null) try { 34 statement.close(); 35 } catch (SQLException throwables) { 36 throwables.printStackTrace(); 37 } 38 if (connection != null) try { 39 connection.close(); 40 } catch (SQLException throwables) { 41 throwables.printStackTrace(); 42 } 43 System.out.println("关闭资源成功"); 44 } 45 }
工具测试类如下
1 package cn.gao.jdbc; 2 3 /** 4 * 用JdbcUtil封装加载驱动和关闭资源,避免每次重复写加载驱动,获取连接和statement和关闭资源的操作。 5 */ 6 public class JdbcDemo5 { 7 public static void main(String[] args) { 8 JdbcUtil jdbcUtil = new JdbcUtil(); /*调用JdbcUtil的初始化init()方法*/ 9 jdbcUtil.init(); 10 String sql = "update xxx set xxx=‘xxx‘ where errcode=7"; 11 try { 12 int count = jdbcUtil.statement.executeUpdate(sql); 13 if (count > 0) 14 System.out.println("更新成功,受影响的行数:" + count); 15 else System.out.println("更新失败!"); 16 } catch (Exception throwables) { 17 throwables.printStackTrace(); 18 } 19 /*调用JdbcUtil的关闭close()方法*/ 20 jdbcUtil.close(); 21 } 22 }
这样就节省了许多重复的工作。
或者工具类也可以如下定义(从jdbc.properties配置文件加载数据库url,用户名,密码,驱动)
1 package cn.gao.jdbc; 2 3 import java.io.FileReader; 4 import java.io.IOException; 5 import java.net.URL; 6 import java.sql.*; 7 import java.util.Properties; 8 9 public class JdbcUtil1 { 10 11 private static String driver; 12 private static String url; 13 private static String username; 14 private static String password; 15 16 static { 17 ClassLoader loader = JdbcUtil1.class.getClassLoader(); 18 URL resource = loader.getResource("jdbc.properties"); 19 String path = resource.getPath(); 20 Properties prop = new Properties(); 21 try { 22 prop.load(new FileReader(path)); 23 } catch (IOException e) { 24 e.printStackTrace(); 25 } 26 driver = prop.getProperty("driver"); 27 url = prop.getProperty("url"); 28 username = prop.getProperty("username"); 29 password = prop.getProperty("password"); 30 try { 31 Class.forName(driver); 32 } catch (ClassNotFoundException e) { 33 e.printStackTrace(); 34 } 35 } 36 37 public static Connection getConnection() { 38 try { 39 return DriverManager.getConnection(url, username, password); 40 } catch (SQLException throwables) { 41 throwables.printStackTrace(); 42 } 43 return null; 44 } 45 46 public static void close(Statement stat, Connection conn) { 47 if (stat != null) { 48 try { 49 stat.close(); 50 } catch (SQLException throwables) { 51 throwables.printStackTrace(); 52 } 53 } 54 if (conn != null) { 55 try { 56 conn.close(); 57 } catch (SQLException throwables) { 58 throwables.printStackTrace(); 59 } 60 } 61 } 62 63 public static void close(ResultSet rs, Statement stat, Connection conn) { 64 if (rs != null) { 65 try { 66 rs.close(); 67 } catch (SQLException throwables) { 68 throwables.printStackTrace(); 69 } 70 } 71 if (stat != null) { 72 try { 73 stat.close(); 74 } catch (SQLException throwables) { 75 throwables.printStackTrace(); 76 } 77 } 78 if (conn != null) { 79 try { 80 conn.close(); 81 } catch (SQLException throwables) { 82 throwables.printStackTrace(); 83 } 84 } 85 } 86 }
测试类如下:
1 package cn.gao.jdbc; 2 3 import java.sql.Connection; 4 import java.sql.ResultSet; 5 import java.sql.SQLException; 6 import java.sql.Statement; 7 8 public class JdbcDemo7 { 9 public static void main(String[] args) { 10 Connection connection = null; 11 Statement statement =null; 12 ResultSet resultSet =null; 13 try { 14 connection = JdbcUtil1.getConnection(); 15 statement = connection.createStatement(); 16 String sql = "select * from xxx"; 17 resultSet = statement.executeQuery(sql); 18 while (resultSet.next()) { 19 String errcode = resultSet.getString("errcode"); 20 String meaning = resultSet.getString("meaning"); 21 String description = resultSet.getString("description"); 22 System.out.println("errcode:" + errcode + ",meaning:" + meaning + ",description:" + description); 23 } 24 25 } catch (SQLException throwables) { 26 throwables.printStackTrace(); 27 } 28 finally { 29 JdbcUtil1.close(resultSet, statement, connection); 30 } 31 32 33 } 34 }
Druid连接池的导入
1 package cn.gao.datasource; 2 3 import com.alibaba.druid.pool.DruidDataSourceFactory; 4 5 import javax.sql.DataSource; 6 import java.io.InputStream; 7 import java.sql.Connection; 8 import java.sql.PreparedStatement; 9 import java.util.Properties; 10 11 /** 12 * Druid连接池Demo 13 */ 14 15 public class DruidDemo { 16 public static void main(String[] args) throws Exception { 17 Properties prop = new Properties(); 18 InputStream is = DruidDemo.class.getClassLoader().getResourceAsStream("druid.properties"); 19 prop.load(is); 20 DataSource dataSource = DruidDataSourceFactory.createDataSource(prop); 21 String sql="select * from xxxx"; 22 Connection connection = dataSource.getConnection(); 23 PreparedStatement preparedStatement = connection.prepareStatement(sql); 24 boolean execute = preparedStatement.execute(); 25 System.out.println(execute); 26 preparedStatement.close(); 27 connection.close(); 28 29 } 30 }
# druid.properties文件的配置
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/mydata?useSSL=false
username=root
password=xxxx
# 初始化连接数量
initialSize=5
# 最大连接数
maxActive=10
# 最大超时时间
maxWait=3000
Druid连接池也是需要每次从获取连接和关闭资源,因此创造一个DruidUtils工具类,将获取数据源、连接和关闭资源的方法都封装到工具类中
1 package cn.gao.datasource; 2 3 import com.alibaba.druid.pool.DruidDataSourceFactory; 4 5 import javax.sql.DataSource; 6 import java.io.IOException; 7 import java.io.InputStream; 8 import java.sql.*; 9 import java.util.Properties; 10 11 public class DruidUtils { 12 //定义DataSource对象 13 private static DataSource ds; 14 static { 15 try { 16 //定义Properties对象 17 Properties prop = new Properties(); 18 //定义字节输入流对象,并从druid.properties文件读取数据 19 InputStream is = DruidDemo.class.getClassLoader().getResourceAsStream("druid.properties"); 20 //Properties对象load字节输入流对象 21 prop.load(is); 22 //使用DruidDataSourceFactory工厂创建DataSource 23 ds = DruidDataSourceFactory.createDataSource(prop); 24 } catch (IOException e) { 25 e.printStackTrace(); 26 } catch (Exception e) { 27 e.printStackTrace(); 28 } 29 30 } 31 32 /** 33 * 34 * @return 数据源(DataSource)对象 35 */ 36 public static DataSource getDataSource(){ 37 return ds; 38 } 39 40 /** 41 * 42 * @return 连接(Connection)对象 43 * @throws SQLException 44 */ 45 public static Connection getConnection() throws SQLException { 46 return ds.getConnection(); 47 } 48 49 /** 50 * 51 * @param preparedStatement PreparedStatement对象 52 * @param connection Connection对象 53 */ 54 public static void close(PreparedStatement preparedStatement, Connection connection) { 55 // if(preparedStatement!=null){ 56 // try { 57 // preparedStatement.close(); 58 // } catch (SQLException throwables) { 59 // throwables.printStackTrace(); 60 // } 61 // } 62 // if(connection!=null){ 63 // try { 64 // connection.close(); 65 // } catch (SQLException throwables) { 66 // throwables.printStackTrace(); 67 // } 68 // } 69 close(null, preparedStatement, connection); 70 71 } 72 73 /** 74 * 75 * @param resultSet 结果集对象 76 * @param preparedStatement PreparedStatement对象 77 * @param connection Connection对象 78 */ 79 public static void close(ResultSet resultSet, PreparedStatement preparedStatement, Connection connection) { 80 if (resultSet != null) { 81 try { 82 resultSet.close(); 83 } catch (SQLException throwables) { 84 throwables.printStackTrace(); 85 } 86 87 if (preparedStatement != null) { 88 try { 89 preparedStatement.close(); 90 } catch (SQLException throwables) { 91 throwables.printStackTrace(); 92 } 93 } 94 if (connection != null) { 95 try { 96 connection.close(); 97 } catch (SQLException throwables) { 98 throwables.printStackTrace(); 99 } 100 } 101 102 } 103 } 104 }
实际的类中,只需要获取连接和执行sql
1 package cn.gao.datasource; 2 3 import java.sql.Connection; 4 import java.sql.PreparedStatement; 5 import java.sql.SQLException; 6 7 public class DruidUtilsDemo { 8 public static void main(String[] args) { 9 Connection connection = null; 10 PreparedStatement preparedStatement = null; 11 try { 12 //获取连接 13 connection = DruidUtils.getConnection(); 14 //定义SQL 15 String sql = "insert into myerrorcode values(?,?,?)"; 16 //获取执行Sql对象 17 preparedStatement = connection.prepareStatement(sql); 18 preparedStatement.setString(1, "xxx"); 19 preparedStatement.setString(2, "xxx"); 20 preparedStatement.setString(3, "xxx"); 21 //执行sql 22 int count = preparedStatement.executeUpdate(); 23 System.out.println(count); 24 } catch (SQLException throwables) { 25 throwables.printStackTrace(); 26 } finally { 27 //关闭资源 28 DruidUtils.close(preparedStatement, connection); 29 } 30 } 31 }
由于上面的jdbc连接是每次获取连接,每次获取连接仍然是有些麻烦,因此使用JdbcTemplate来解决,JdbcTemplate需要一个DataSource,正好DruidUtils中有这个方法,传递到JdbcTemplate对象中。然后定义和执行Sql即可,也无需关闭资源,JdbcTemplate会自己释放。
1 package cn.gao.jdbctemplate; 2 3 import cn.gao.datasource.DruidUtils; 4 import org.springframework.jdbc.core.JdbcTemplate; 5 6 import javax.sql.DataSource; 7 8 public class JdbcTemplateDemo1 { 9 public static void main(String[] args) { 10 //使用工具类获取DataSource对象 11 DataSource dataSource = DruidUtils.getDataSource(); 12 //创建JdbcTemplate对象,传递DataSource对象参数 13 JdbcTemplate template = new JdbcTemplate(dataSource); 14 //定义Sql语句 15 String sql = "UPDATE xxx SET xxx=? WHERE xxx=?"; 16 //执行Sql语句,获取打印执行结果 17 int count = template.update(sql, "xxxxxxxx", "xxxx"); 18 System.out.println(count); 19 } 20 }
这样代码就比较简单了。
标签:获取 demo1 12px 操作 连接数量 exec 数据库 jar包 prepare
原文地址:https://www.cnblogs.com/javagao/p/13842844.html