你在连接数据库是不是首先得指明所要连接的数据库名吗?(无论jdbc或jdbc-odbc桥),
所以use MY_db go 已提前指明。
如果要连接其他数据库,可以用同样的方法,在连接是指明数据库。
然后用相应的查询语句即可!
如:try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
conn=DriverManager.getConnection(url,user,password);
//Connection conn= DriverManager.getConnection(url,user,password);
//Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
}
catch(Exception e2){
System.err.println("SWLException: "+e2.getMessage());
e2.printStackTrace();
}
String url="jdbc:microsoft:sqlserver://202.119.9.95:1433;DatabaseName=myDB";//这里便是所要连接的数据库名
String user="sa";
String password="sa";调用Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=stmt.executeQuery(sql);
可执行查询!
所以use MY_db go 已提前指明。
如果要连接其他数据库,可以用同样的方法,在连接是指明数据库。
然后用相应的查询语句即可!
如:try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
conn=DriverManager.getConnection(url,user,password);
//Connection conn= DriverManager.getConnection(url,user,password);
//Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
}
catch(Exception e2){
System.err.println("SWLException: "+e2.getMessage());
e2.printStackTrace();
}
String url="jdbc:microsoft:sqlserver://202.119.9.95:1433;DatabaseName=myDB";//这里便是所要连接的数据库名
String user="sa";
String password="sa";调用Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=stmt.executeQuery(sql);
可执行查询!
url="jdbc:microsoft:sqlserver://202.119.9.95:1433;DatabaseName=MY_db";
然后sql="select * from col1234";即可。
不要想着把数据库中的所有语句都运用到java中,它只能执行像select、insert、update、delete这几种语句。