JDBC不指定具体数据库的数据库连接

来源:互联网 发布:巴洛特利 知乎 编辑:程序博客网 时间:2024/06/01 10:14

JDBC能实现先连接数据库再选择数据库,可以使用"use db"选择数据库,代码如下

package com.mis.test;

import java.sql.DriverManager;

import javax.sql.rowset.JdbcRowSet;

import com.mysql.jdbc.Connection;
import com.sun.rowset.JdbcRowSetImpl;

/**
 * JDBC不指定具体数据库的数据库连接
 * @author cl
 *
 */
public class ChooseDb {
 @SuppressWarnings("resource")
 public static void main(String[] args)  {
  
  try {
   Class.forName("com.mysql.jdbc.Driver");
   Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost", "root", "root");
   JdbcRowSet jrs = new JdbcRowSetImpl(conn);
   //执行选择数据库操作
   jrs.setCommand("use st");   
   jrs.execute();
   jrs.setCommand("select * from t_user");
   jrs.execute();
   while(jrs.next()){
    System.out.println(jrs.getString(2)+"\t"+jrs.getString(3));
   }   
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
}

0 0