zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    使用node.js搭建一个简单的后台服务
    node.js连接MySQL数据库
    js将date对象转换为指定格式
    配置angular2运行环境
    简单AJAX请求JSon数据
    正则表达式
    javascript typeof 和 instanceof 的区别和联系
    map和reduce函数的使用
    Github 上管理项目
    微服务资料
  • 原文地址:https://www.cnblogs.com/weaming/p/4502796.html
Copyright © 2011-2022 走看看