java连接数据库

来源:互联网 发布:手机ip电话软件 编辑:程序博客网 时间:2024/06/05 15:25
配置文件 /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);
        }
           
    }
}

原创粉丝点击