This article is a mirror article of machine translation, please click here to jump to the original article.

View: 15741|Reply: 7

[Tips] JDBC Tools (DBUtils.java & SqlHelp.java & dbinfo.properties)

[Copy link]
Posted on 11/17/2014 9:35:20 PM | | |
  1. package com.zjw.jdbc;

  2. import java.io.InputStream;
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.util.Properties;

  9. public class DBUtils {
  10.         static Connection ct = null;
  11.         
  12.         // 连接数据库参数
  13.         private static String url = "";
  14.         private static String username = "";
  15.         private static String driver = "";
  16.         private static String password = "";

  17.         // 读配置文件
  18.         private static Properties pp = null;
  19.         private static InputStream fis = null;

  20.         static {
  21.                 // 加载驱动,只需要一次
  22.                 try {
  23.                         pp = new Properties();
  24.                         //fis = new FileInputStream("src/dbinfo.properties");
  25.                         //在java web中,读取文件要使用类加载器,资源目录默认src
  26.                         fis=SqlHelp.class.getClassLoader().getResourceAsStream("com/zjw/jdbc/dbinfo.properties");
  27.                         pp.load(fis);
  28.                         //从属性文件中读取信息
  29.                         url = pp.getProperty("url");
  30.                         username = pp.getProperty("username");
  31.                         driver = pp.getProperty("driver");
  32.                         password = pp.getProperty("password");
  33.                         Class.forName(driver);
  34.                 } catch (Exception e) {
  35.                         e.printStackTrace();
  36.                         System.exit(-1);
  37.                 }
  38.         }

  39.         // 得到连接
  40.         public static Connection getConnection() {
  41.                
  42.                         try {
  43.                                 ct= DriverManager.getConnection(url, username, password);
  44.                         } catch (SQLException e) {
  45.                                 // TODO Auto-generated catch block
  46.                                 e.printStackTrace();
  47.                         }
  48.                         return ct;
  49.                
  50.         }
  51.         
  52.         //关闭连接
  53.         public static void closeConn(ResultSet rs,PreparedStatement ps,Connection ct) {
  54.                
  55.                         if(rs!= null)
  56.                                 try {
  57.                                         rs.close();
  58.                                 } catch (SQLException e) {
  59.                                         // TODO Auto-generated catch block
  60.                                         e.printStackTrace();
  61.                                 }
  62.             if(ps!= null)
  63.                                 try {
  64.                                         ps.close();
  65.                                 } catch (SQLException e) {
  66.                                         // TODO Auto-generated catch block
  67.                                         e.printStackTrace();
  68.                                 }
  69.             if(ct != null)
  70.                                 try {
  71.                                         ct.close();
  72.                                 } catch (SQLException e) {
  73.                                         // TODO Auto-generated catch block
  74.                                         e.printStackTrace();
  75.                                 }
  76.                         
  77.         }

  78. }



  79. /*
  80. * 主要用于对数据库的crud操作
  81. */
  82. package com.zjw.jdbc;

  83. import java.io.*;
  84. import java.sql.*;
  85. import java.util.*;

  86. /**
  87. * @author Delver_Si
  88. *
  89. */
  90. public class SqlHelp {
  91.         private Connection ct = null;
  92.         private PreparedStatement ps = null;

  93.         private ResultSet rs = null;

  94.         
  95.         // 查询方法,得到ArrayList集合
  96.         public ArrayList<Object[]> executeQuery(String sql,String...paras) {//...可变参数
  97.                
  98.                 ArrayList<Object[]> al = new ArrayList<Object[]>();
  99.                 try {
  100.                         ct=DBUtils.getConnection();
  101.                         ps = ct.prepareStatement(sql);
  102.                         for(int i=0;i<paras.length;i++){
  103.                                 ps.setString(i+1, paras);
  104.                         }
  105.                         rs = ps.executeQuery();
  106.                         
  107.                         ResultSetMetaData rsmd=rs.getMetaData();
  108.                         //返回结果集有多少列
  109.                         int columNum= rsmd.getColumnCount();
  110.                         while(rs.next()){
  111.                                 Object[] objects=new Object[columNum];
  112.                         for(int i=0;i<objects.length;i++){
  113.                                 objects=rs.getObject(i+1);
  114.                         }
  115.                         al.add(objects);
  116.                         }

  117.                 } catch (SQLException e) {
  118.                         // TODO Auto-generated catch block
  119.                         e.printStackTrace();
  120.                 }finally{
  121.                         
  122.                         DBUtils.closeConn(rs, ps, ct);
  123.                 }
  124.                
  125.                
  126.                 return al;

  127.         }
  128.         
  129.         //增删改方法
  130.         public int executeUpdate(String sql) {
  131.                 int i=0;
  132.                 // 获得连接
  133.                 try {
  134.                         ct=DBUtils.getConnection();
  135.                         // 创建一个基于该连接的语句对象
  136.                         ps = ct.prepareStatement(sql);
  137.                         // 执行sql语句
  138.                         i =ps.executeUpdate();
  139.                         
  140.                 } catch (SQLException e) {
  141.                         e.printStackTrace();
  142.                 } finally {
  143.                         DBUtils.closeConn(rs, ps, ct);
  144.                 }
  145.                 return i;
  146.         }
  147. }



  148. dbinfo.properties.bak(oracle )

  149. url=jdbc:oracle:thin:@127.0.0.1:1521:ORCL
  150. username=scott
  151. driver=oracle.jdbc.driver.OracleDriver
  152. password=tiger
Copy code





Previous:Tired and sleepy, have fun
Next:Some personal suggestions for the forum!
Posted on 11/17/2014 9:57:29 PM |
Java Operations Oracle Database?
 Landlord| Posted on 11/17/2014 10:05:45 PM |
admin posted on 2014-11-17 21:57
Java Operations Oracle Database?

Well................
Posted on 11/17/2014 10:06:20 PM |

Well, we have only learned SQL Server now, and other databases have not learned for the time being
 Landlord| Posted on 11/17/2014 10:07:30 PM |
admin posted on 2014-11-17 22:06
Well, we have only learned SQL Server now, and other databases have not learned for the time being

Can I say that the teacher only talked about the addition, deletion, modification and check of sqlserver today?
Posted on 11/17/2014 10:07:59 PM |
Delver_Si Posted on 2014-11-17 22:07
Can I say that the teacher only talked about the addition, deletion, modification and check of sqlserver today?

。。。。。 Can I say we've learned all about SQL Server?
Posted on 11/17/2014 10:34:08 PM |
The landlord looks amazing
Posted on 11/17/2014 10:50:34 PM |
Learning Java is not easy
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com