手把手教你连接三大数据库JDBC

来源:互联网 发布:扫码收款软件 编辑:程序博客网 时间:2024/06/05 18:54
(1).连接mysql数据库的时候连接是:
publicclassMyslqJDBC {
     publicConnection getConnection()
     {
          Connectionconnection=null;
          try{
              Class.forName("com.mysql.jdbc.Driver");
               connection=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/mapdata","root","qunqun");
          }catch(ClassNotFoundExceptione) {            
              e.printStackTrace();
          }catch(SQLExceptione) {   
              e.printStackTrace();
          }    
          returnconnection;
     }
     publicvoidcloseconn(Connectionconnection)
     {    
          try{
              connection.close();
          }catch(SQLExceptione) {
              e.printStackTrace();
          }
     }
}
(2).连接sqlserver数据库时候的连接是:
 //记载数据库驱动
   publicvoidload_qudong(){
     try{
          Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");                                  
          System.out.println("成功加载SQL驱动程序");                              
          }                              
          catch(Exceptione){
               System.out.println(e.getMessage()+"\n找不到SQL驱动程序");                                                            
           }  
     }
   //返回Statement对象,用于sql语句的使用
     publicjava.sql.Statement connect(){
          try{
          //连接数据库
          con=DriverManager.getConnection(url);
          //建立Statement对象
          state=con.createStatement();
          }catch(Exceptione){
              System.out.println(e.getMessage()+"连接出错");
          }
          returnstate;
     }
(3).连接oracle数据库的JDBC:
/**
 * 一个非常标准的连接Oracle数据库的示例代码
 
*/public void testOracle()
{
    Connection con 
null;// 创建一个数据库连接
    PreparedStatement pre = null;// 创建预编译语句对象,一般都是用这个而不用Statement
    ResultSet result = null;// 创建一个结果集对象
    try
    {
        Class.forName(
"oracle.jdbc.driver.OracleDriver");// 加载Oracle驱动程序
        System.out.println("开始尝试连接数据库!");
        String url 
= "jdbc:oracle:" + "thin:@127.0.0.1:1521:XE";// 127.0.0.1是本机地址,XE是精简版Oracle的默认数据库名
        String user = "system";// 用户名,系统默认的账户名
        String password = "147";// 你安装时选设置的密码
        con = DriverManager.getConnection(url, user, password);// 获取连接
        System.out.println("连接成功!");
        String sql 
= "select * from student where name=?";// 预编译语句,“?”代表参数
        pre = con.prepareStatement(sql);// 实例化预编译语句
        pre.setString(1, "刘显安");// 设置参数,前面的1表示参数的索引,而不是表中列名的索引
        result = pre.executeQuery();// 执行查询,注意括号中不需要再加参数
        while (result.next())
           
// 当结果集不为空时
            System.out.println("学号:" + result.getInt("id") + "姓名:"
                    + result.getString("name"));
    }
   
catch (Exception e)
    {
        e.printStackTrace();
    }
   
finally
    {
       
try
        {
           
// 逐一将上面的几个对象关闭,因为不关闭的话会影响性能、并且占用资源
           
// 注意关闭的顺序,最后使用的最先关闭
            if (result != null)
                result.close();
           
if (pre != null)
                pre.close();
           
if (con != null)
                con.close();
            System.out.println(
"数据库连接已关闭!");
        }
       
catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}
0 0
原创粉丝点击