连接时如何选择要访问的数据库和表
用java来连接SQL 2000,有一句是 String url="jdbc:odbc:Person";
我建了一个数据库,叫MyDB;
MyDB里面有一个表Person;
我给数据源也取名叫Person;
我更改默认的数据库为MyDB了;
请问:
1.String url="jdbc:odbc:Person";中的Person是数据源的名字吗?还是表的名字?
2.更改默认的数据库为MyDB了,这样有什么后果?
3.源代码中(见下),至始至终没有出现MyDB的字样,也没有说是对Person表进行操作,程序是怎么找到MyDB,怎么找到Person表进行操作的?
更改默认的数据库为MyDB,但是如果MyDB中有多个表的话,怎么选择某个表来访问?
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.io.IOException;
/**
* <p>Title: JAVA连接SQL Server数据库的简单例子</p>
* <p>Description: 第四章例子</p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: </p>
* @author 苏年乐
* @version 1.0
*/
public class ConnectionDemo {
public ConnectionDemo() {
}
void display() {
Connection con = null;
String url = "jdbc:odbc:Person";
try {
//装入驱动程序
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//建立连接
con = DriverManager.getConnection(url);
//创建语句
Statement stmt = con.createStatement();
//用于保存查询结果的变量声明与定义
int id;
String text;
int age;
//建立ResultSet
ResultSet rs;
String strSQL= "Select * From Person";
rs = stmt.executeQuery(strSQL);
if (rs.next()) {
do {
id=rs.getInt(1);
System.out.print("ID:" + id);
text = rs.getString(2);
System.out.print(" 姓名:" + text);
text = rs.getString(3);
System.out.print(" 性别:" + text);
age = rs.getInt("AGE");
System.out.print(" 年龄:" + age);
text = rs.getString("ADDRESS");
System.out.print(" 地址:" + text);
text = rs.getString(6);
System.out.println(" 电话:" + text);
}
while (rs.next());
}
else {
System.out.println("没有数据");
}
} //end of try
catch (ClassNotFoundException e) {
System.out.println(e);
}
catch (SQLException e) {
System.out.println(e);
}
finally {
try {
//关闭连接
if (con != null) {
con.close();
}
}
catch (SQLException e) {
System.out.println(e);
}
}
}
public static void main(String[] args) {
ConnectionDemo app = new ConnectionDemo();
app.display();
}
}