jdpc连接数据库

来源:互联网 发布:qq windows xp 编辑:程序博客网 时间:2024/06/15 17:12
  1.       package com.test.db;  
  2.   
  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. /** 
  9.  * @(#)DataBase.java 010/04/22 
  10.  * @author Dudu 
  11.  * EST.All rights reserved 
  12.  */  
  13. public class DataBase {  
  14.     /**定义一个Connection 用来连接数据库*/  
  15.     private Connection conn=null;  
  16.     /**连接数据库的URL*/  
  17.     private final String url="jdbc:mysql://localhost/lyvee";  
  18.     /**指定数据的用户名和密码*/  
  19.     private final String username="root";  
  20.     private final String password="123";  
  21.     /**定义一个int记录更新的记录数量*/  
  22.     int count=0;  
  23.     /**定义一个结果集 用于放回查询结果*/  
  24.     private ResultSet resultSet=null;  
  25.     private PreparedStatement pstmt=null;  
  26.     public DataBase(){  
  27.         conn = connectionDB();  
  28.     }  
  29.     /** 
  30.      * 建立数据的连接 
  31.      * @exception SQLException, ClassNotFoundException 
  32.      */  
  33.     @SuppressWarnings("finally")  
  34.     public Connection connectionDB(){  
  35.         try{  
  36.             Class.forName("com.mysql.jdbc.Driver");  
  37.             conn=DriverManager.getConnection(url,username,password);  
  38.             System.out.println("连接数据库成功");  
  39.         }catch(Exception e){  
  40.             e.printStackTrace();  
  41.             System.out.println("建立数据库发生错误!");  
  42.         }finally{  
  43.             return conn;  
  44.         }  
  45.     }  
  46.     /** 
  47.      * 查询方法 
  48.      * @param sql查询sql语句 
  49.      * @return resultSet 
  50.      */  
  51.     @SuppressWarnings("finally")  
  52.     public ResultSet query(String sql){  
  53.         try {  
  54.             pstmt = conn.prepareStatement(sql);  
  55.             /**查询*/  
  56.             resultSet = pstmt.executeQuery();  
  57.         } catch (SQLException e) {  
  58.             e.printStackTrace();  
  59.         }finally{  
  60.             return resultSet;  
  61.         }  
  62.     }  
  63.     /** 
  64.      * 更新数据 
  65.      * @param sql 更新sql语句 
  66.      * @return 
  67.      */  
  68.     public int update(String sql){  
  69.         try {  
  70.             pstmt = conn.prepareStatement(sql);  
  71.             count=pstmt.executeUpdate();  
  72.         } catch (SQLException e) {  
  73.             e.printStackTrace();  
  74.             System.out.println("执行更新出错了");  
  75.         }  
  76.           
  77.         return count;  
  78.           
  79.     }  
  80.     /**关闭连接*/  
  81.     public boolean coles(){  
  82.         boolean isColes = false;  
  83.         if(resultSet!=null){  
  84.             try {  
  85.                 resultSet.close();  
  86.                 resultSet=null;  
  87.                 isColes=true;  
  88.             } catch (SQLException e) {  
  89.                 isColes=false;  
  90.                 e.printStackTrace();  
  91.                 System.out.println("关闭结果集发生错误");  
  92.             }  
  93.         }  
  94.         if(pstmt!=null){  
  95.             try {  
  96.                 pstmt.close();  
  97.                 pstmt=null;  
  98.                 isColes=true;  
  99.             } catch (SQLException e) {  
  100.                 isColes=false;  
  101.                 e.printStackTrace();  
  102.                 System.out.println("关闭pstmt发生异常");  
  103.             }  
  104.         }  
  105.         if(conn!=null){  
  106.             try{  
  107.                 conn.close();  
  108.                 conn=null;  
  109.                 isColes=true;  
  110.             }catch (Exception e) {  
  111.                 isColes=false;  
  112.                 e.printStackTrace();  
  113.                 System.out.println("关闭conn发生异常");  
  114.             }  
  115.         }  
  116.         return isColes;  
  117.     }  
  118.     /** 
  119.      * 测试查询的方法 
  120.      * @throws SQLException 
  121.      */  
  122.     public void testQuery() throws SQLException{  
  123.         resultSet =query("select * from lyvee");  
  124.         if(resultSet.next()){  
  125.             System.out.println(resultSet.getString(1));  
  126.             System.out.println(resultSet.getString(3));  
  127.             System.out.println(resultSet.getString(2));  
  128.         }  
  129.     }  
  130.     public void testUpdate(){  
  131.         count = update("insert into lyvee(lname,lpwd,lsex)values('tanliang','aaa111','sex')");  
  132.         if(count>0){  
  133.             System.out.println("更新成功");  
  134.         }  
  135.     }  
  136.     /** 
  137.      *  
  138.      * @param args 
  139.      * @throws SQLException 
  140.      * @throws ClassNotFoundException 
  141.      */  
  142.     public static void main(String[] args) throws SQLException, ClassNotFoundException {  
  143.         DataBase db = new DataBase();  
  144.         /**调用查询方法*/  
  145.         //db.testQuery();  
  146.         /**调用更新方法*/  
  147.         db.testUpdate();  
  148.         /**调用关闭连接方法*/  
  149.         db.coles();  
  150.           
  151.     }  
  152. }  
0 0
原创粉丝点击