-2

我需要有关此代码的帮助以建立与数据库的连接。

try
{
String str="SELECT * FROM Author WHERE city LIKE 'P%'";
 Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con =
DriverManager.getConnection     
("jdbc:odbc:myds;databaseName=Library;user=sa;password=password123");
Statement  =con.createStatement();
ResultSet rs= executeQuery();
System.out.println("Author ID\tAuthor Name\tCity");
while (rs.next())
{
String id=rs.getString("au_id");
String name=rs.getString("au_name");
String city=rs.getString("city");
System.out.print(id+"\t");
if (name.length() <=7)
System.out.print(name+"\t\t");
else
System.out.print("\t"+name+"\t");
System.out.println(city);
}
}
catch(Exception ex)
{
System.out.println("Error occurred");
System.out.println("Error:"+ex); 

上面的代码有什么问题?

4

1 回答 1

1

我想应该是这样的

String str="SELECT * FROM Author WHERE city LIKE 'P%'";
// Some stuff 
Statement stmt = con.createStatement(); //stmt added
// Some more stuff 
ResultSet rs = stmt.executeQuery(str); // stmt and str added

您应该查看使用 Java 应用程序执行 SQL 查询。

于 2013-04-24T23:49:02.010 回答