java连接数据库

配置文件 /Stu_PMS/src/config/data.properties
  DRIVER=com.mysql.jdbc.Driver
  CONSTR=jdbc:mysql: //127.0.0.1:3306/test
  USERNAME=root
  PASS=mysql
配置文件驱动 /Stu_PMS/src/com/config/ProMgr.java
package  JDBC;
import  java.util.*;
public  class  ProMgr {
     static  Properties pro =  new  Properties();
       
     static  {
           
         try {
             pro.load(ProMgr. class .getResourceAsStream( "data.properties" )); //配置文件位于同一个目录下的写法
             //配置文件位于不同目录下的写法:pro.load(ProMgr.class.getClassLoader().getResourceAsStream("config/data.properties"));
         } catch (Exception e){
             e.printStackTrace();
         }
           
     };
       
     public  static  String getProperty(String key){
         return  pro.getProperty(key);
     }
       
     public  static  void  main(String[] args)
     {
         System.out.println(getProperty( "CONSTR" ));
     }
}
  
数据库驱动文件 /Stu_PMS/src/com/data/DB.java
package  JDBC;
import  java.sql.Connection;
import  java.sql.DriverManager;
import  java.sql.PreparedStatement;
import  java.sql.ResultSet;
import  java.sql.SQLException;
import  java.sql.Statement;
public  class  DB {
     public  static  Connection getConn(){
         Connection conn =  null ;
         String DRIVER =  "com.mysql.jdbc.Driver" ;
         String CONSTR = ProMgr.getProperty( "CONSTR" );
         String USERNAME = ProMgr.getProperty( "USERNAME" );
         String PASS = ProMgr.getProperty( "PASS" );
         try  {
             Class.forName(DRIVER);
             conn = DriverManager.getConnection(CONSTR,USERNAME,PASS);
         catch  (ClassNotFoundException e) {
             e.printStackTrace();
         catch  (SQLException e) {
             e.printStackTrace();
        
         return  conn;
     }
       
     public  static  Statement getSta(Connection conn){
         Statement st =  null ;
         try  {
             st = conn.createStatement();
         catch  (SQLException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
         }
         return  st;
     }
       
     public  static  ResultSet execQuery(Statement stmt,String sql){
         ResultSet rs =  null ;
         try  {
             rs = stmt.executeQuery(sql);
         catch  (SQLException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
         }
         return  rs;
     }
       
     public  static  void  close(Connection conn){
         if (conn!= null ){
             try  {
                 conn.close();
             catch  (SQLException e) {
                 // TODO Auto-generated catch block
                 e.printStackTrace();
             }
         }
     }
       
     public  static  void  close(ResultSet rs){
         if (rs!= null ){
             try  {
                 rs.close();
             catch  (SQLException e) {
                 // TODO Auto-generated catch block
                 e.printStackTrace();
             }
         }
     }
       
     public  static  void  close(Statement stmt){
         if (stmt!= null ){
             try  {
                 stmt.close();
             catch  (SQLException e) {
                 // TODO Auto-generated catch block
                 e.printStackTrace();
             }
         }
     }
       
     public  static  PreparedStatement getPrestat(Connection conn,String sql){
         PreparedStatement pstmt =  null ;
         try  {
             pstmt = conn.prepareStatement(sql);
         catch  (SQLException e) {
             e.printStackTrace();
         }
         return  pstmt;
     }
       
}
调用示例
package  JDBC;
import  java.sql.Connection;
import  java.sql.PreparedStatement;
import  java.sql.ResultSet;
import  java.sql.SQLException;
import  java.sql.Statement;
public  class  Test {
     public  static  void  main(String[] args)
     {
         delete();
         //update();
         //add();
         //query();
     }
       
     static  void   add()
     {
         int  count;
         String sql =  "insert into user(name,age) values(?,?)" ;
         Connection conn = DB.getConn();
         PreparedStatement pstmt = DB.getPrestat(conn, sql);
         try  {
             pstmt.setString( 1 "owen2" );
             pstmt.setString( 2 "21" );
             count=pstmt.executeUpdate();
             System.out.println( "影响条数为:" +count);
       
         catch  (SQLException e) {
             DB.close(pstmt);
             DB.close(conn);
             e.printStackTrace();
         } finally {
             DB.close(pstmt);
             DB.close(conn);
         }
           
     }
       
     static  void   query()
     {
         ResultSet rs =  null ;
         String sql =  "select * from user where name=? and age=?" ;
         Connection conn = DB.getConn();
         PreparedStatement pstmt = DB.getPrestat(conn, sql);
         try  {
             pstmt.setString( 1 "刘书" );
             pstmt.setString( 2 "21" );
             rs = pstmt.executeQuery();
             while (rs.next())
             {
                 System.out.println( "name:"  + rs.getString( "name" ) +  "---age:"  + rs.getString( "age" ));
             }
         catch  (SQLException e) {
             DB.close(rs);
             DB.close(pstmt);
             DB.close(conn);
             e.printStackTrace();
         } finally {
             DB.close(rs);
             DB.close(pstmt);
             DB.close(conn);
         }
           
     }
       
     static  void   update()
     {
         int  count;
         String sql =  "update user set name=?,age=? where name=?" ;
         Connection conn = DB.getConn();
         PreparedStatement pstmt = DB.getPrestat(conn, sql);
         try  {
             pstmt.setString( 1 "owenupdate" );
             pstmt.setString( 2 "22" );
             pstmt.setString( 3 "owen" );
             count=pstmt.executeUpdate();
             System.out.println( "影响条数为:" +count);
       
         catch  (SQLException e) {
             DB.close(pstmt);
             DB.close(conn);
             e.printStackTrace();
         } finally {
             DB.close(pstmt);
             DB.close(conn);
         }
           
     }
       
     static  void   delete()
     {
         int  count;
         String sql =  "delete from user where name=?" ;
         Connection conn = DB.getConn();
         PreparedStatement pstmt = DB.getPrestat(conn, sql);
         try  {
             pstmt.setString( 1 "owen2update" );
             count=pstmt.executeUpdate();
             System.out.println( "影响条数为:" +count);
       
         catch  (SQLException e) {
             DB.close(pstmt);
             DB.close(conn);
             e.printStackTrace();
         } finally {
             DB.close(pstmt);
             DB.close(conn);
         }
           
     }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值