Fork me on GitHub

JDBC连接数据库

工具类源码

import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

import sun.net.smtp.SmtpClient;

import com.mysql.jdbc.Connection;

public class DbUtil{
    //驱动名
    public static String jdbcName = "com.mysql.jdbc.Driver";
    //数据库地址
    private static String dbtype = "mysql";
    private static String dbip = "127.0.0.1";
    private static int dbport = 3306;
    private static String tablename = "test";
    private static String dbUrl = "jdbc:mysql://127.0.0.1:3306/test";
    //数据库用户名
    private static String dbUserName = "root";
    //数据库密码
    private static String dbPassword = "123456";
    //全局连接变量
    private static java.sql.Connection con = null;
    
    public static String setDbUrl(String type,String ip,int port,String tabname){
        dbtype = type;
        dbip = ip;
        dbport = port;
        tablename = tabname;
        dbUrl = "jdbc:"+dbtype+"://"+dbip+":"+dbport+"/"+tablename;
        return dbUrl;
    }
    
    //连接数据库
    public static java.sql.Connection getCon() {
        try {
            Class.forName(jdbcName);
            System.out.println("加载驱动成功");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            System.out.println("加载驱动失败");
        }

        try {
            con = DriverManager.getConnection(dbUrl, dbUserName, dbPassword);
            System.out.println("数据库连接成功");
        } catch (Exception e) {
            System.out.println("连接数据库失败");
        } 
        return con;
    }
    
    //获得statement连接
    public static java.sql.Statement getStmt(java.sql.Connection con)throws Exception{
        Statement stmt = con.createStatement(); 
        System.out.println("创建Statement成功");
        return stmt;
    }
    
    //关闭数据库连接
    public static void dbClose(java.sql.Statement stmt,java.sql.Connection con)throws Exception{
        if(stmt != null){
            {
            stmt.close();
            System.out.println("Statement已关闭");
            con.close();
            System.out.println("数据库连接已关闭");
            }
        }
    }    
}

 

Demo测试类

import java.sql.Connection;
import java.sql.Statement;

import Util.DbUtil;

public class Demo1 {
    public static void main(String[] args) throws Exception{
        String sql1 = "insert into test values('adsfd','女')";
        String sql2 = "update test set name='asdf',sex='女' where name='rfn4'";
        
        String dbUrl = DbUtil.setDbUrl("mysql", "127.0.0.1", 3306, "test");
        System.out.println("数据库地址是:"+dbUrl);
        
        Connection con= DbUtil.getCon();
        Statement stmt= DbUtil.getStmt(con);        
                
        int count = stmt.executeUpdate(sql1);
        System.out.println("操作了"+count+"条数据");
        DbUtil.dbClose(stmt,con);
    }
}

 参考:

http://blog.csdn.net/llp1992/article/details/40662039

http://www.cnblogs.com/hongten/archive/2011/03/29/1998311.html

posted @ 2015-05-14 11:04  weaming  阅读(311)  评论(0编辑  收藏  举报
 |  Github |  知乎 |  微博  |