码迷,mamicode.com
首页 > 数据库 > 详细

java jdbc操作数据库通用代码

时间:2017-11-29 13:32:09      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:puts   set   round   根据   ring   turn   als   ssl   import   

1.准备工作

1》

技术分享图片

新建一个配置文件,名为jdbc.properties将其放入src中

2》在项目中导入jdbc驱动,注意连接不同的数据库,所用到的驱动是不一样的,这些在网上都能找到

具体导入jar的方法,请参照http://blog.csdn.net/mazhaojuan/article/details/21403717

2、代码

  1 import java.io.InputStream;
  2 import java.sql.Connection;
  3 import java.sql.DriverManager;
  4 import java.sql.ResultSet;
  5 import java.sql.SQLException;
  6 import java.sql.Statement;
  7 import java.util.Properties;
  8 
  9 public class Main {
 10     public static void main(String[] args) {
 11         DBUtil dbUtil = new DBUtil();
 12         dbUtil.R("select * from table");
 13     }
 14 }
 15 
 16 class DBUtil{
 17     /**
 18      * 得到数据库连接
 19      * @return
 20      * @throws Exception
 21      */
 22     public Connection getConnection() throws Exception{
 23         //1.创建配置文件并得到对象输入流
 24         InputStream is = this.getClass().getClassLoader().getResourceAsStream("jdbc.properties.txt");
 25         //2.创建propetities
 26         Properties jdbc = new Properties();
 27         jdbc.load(is);
 28         //3. 通过key-value 的方式得到对应的值
 29         String driver = jdbc.getProperty("driver");
 30         String url = jdbc.getProperty("url");
 31         String user = jdbc.getProperty("user");
 32         String password = jdbc.getProperty("password");
 33         //4.加载运行时类对象
 34         Class.forName(driver);
 35         //5通过DriverManager得到连接
 36         Connection connection = DriverManager.getConnection(url,user,password);
 37         return connection;
 38         
 39     }
 40     /**
 41      * 释放资源的方法
 42      * @param connection
 43      * @param statement      
 44      * @param resultSet
 45      */
 46     public void release(Connection connection,Statement statement,ResultSet resultSet){
 47         try {
 48             if(resultSet!=null){
 49                 resultSet.close();
 50             }
 51             if(statement!=null){
 52                 statement.close();
 53             }
 54             if(connection!=null){
 55                 connection.close();
 56             }
 57         } catch (SQLException e) {
 58             e.printStackTrace();
 59         }
 60         
 61     }
 62     /**
 63      * 查询数据库的方法
 64      * @param sql        字符串,要执行的sql语句  如果其中有变量的话,就用  ‘"+变量+"’
 65      */
 66     public void R(String sql){
 67         Connection connection = null;
 68         Statement statement = null;
 69         ResultSet resultSet = null;
 70         try {
 71             connection = getConnection();
 72             statement = connection.createStatement();
 73             resultSet = statement.executeQuery(sql);
 74             while(resultSet.next()!=false){
 75                 //这里可以执行一些其他的操作
 76                 System.out.println(resultSet.getString(1));
 77             }
 78         } catch (Exception e) {
 79             e.printStackTrace();
 80         }finally {
 81             release(connection, statement, resultSet);
 82         }
 83     }
 84     /**
 85      * 数据库记录增删改的方法
 86      * @param sql        字符串,要执行的sql语句  如果其中有变量的话,就用  ‘"+变量+"’
 87      */
 88     public void CUD(String sql){
 89         Connection connection = null;
 90         Statement statement = null;
 91         ResultSet resultSet = null;
 92         try {
 93             connection = getConnection();
 94             statement = connection.createStatement();
 95             resultSet = statement.executeQuery(sql);
 96             
 97             //这里可以根据返回结果进行判断,该语句是否执行成功
 98             System.out.println(resultSet);
 99         } catch (Exception e) {
100             e.printStackTrace();
101         }finally {
102             release(connection, statement, resultSet);
103         }
104     }
105     
106 }

 

java jdbc操作数据库通用代码

标签:puts   set   round   根据   ring   turn   als   ssl   import   

原文地址:http://www.cnblogs.com/zhuchenglin/p/7919803.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!