2013-04-24 78 views
-2

我需要此代码的帮助才能建立与数据库的连接。到数据库的Java连接不能正常工作

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); 

上述代码有什么问题?

+0

看看请避免大声喊叫。另外,阅读正确使用大写和小写的文本要容易得多。非常感谢您的帮助。 – 2013-04-24 23:43:08

+1

什么是错误信息/例外/等等。? – TheEwook 2013-04-24 23:43:49

+0

嗯,所以有错误信息?它不应该做什么? – 2013-04-24 23:44:05

回答

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 

你应该Execute SQL Queries with Java Application.