当前位置: 技术问答>java相关
求救:JAVA连接数据库问题。在线等
来源: 互联网 发布时间:2015-11-02
本文导语: Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con=DriverManager.getConnection("jdbc:odbc:sqlserver","sa",""); Statement stat=con.createStatement(); result=stat.executeQuery("select vToyName,vToyDescription,siToyQoh,siLowerAge,siUpperAge,siToyWeight from toys"); ...
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:sqlserver","sa","");
Statement stat=con.createStatement();
result=stat.executeQuery("select vToyName,vToyDescription,siToyQoh,siLowerAge,siUpperAge,siToyWeight from toys");
可无法正确显示,给出提示如下:
无效的描述符索引
555555555~~~~~~~~~~~
Connection con=DriverManager.getConnection("jdbc:odbc:sqlserver","sa","");
Statement stat=con.createStatement();
result=stat.executeQuery("select vToyName,vToyDescription,siToyQoh,siLowerAge,siUpperAge,siToyWeight from toys");
可无法正确显示,给出提示如下:
无效的描述符索引
555555555~~~~~~~~~~~
|
是不是你的sql语句有毛病呀,你在sql server里面先试试这个语句,看看能不能运行....
|
//----------- JDBC-----------
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:MyClass");
Statement stmt = con.createStatement();
ResultSet result = stmt.executeQuery("select * from Class");
while(result.next()){
String name = result.getString("姓名");
Date Birthday= result.getDate("生日");
int phone = result.getInt("电话");
}
con.close();
}catch(Exception e){
System.out.println(e.toString());
}
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:MyClass");
Statement stmt = con.createStatement();
ResultSet result = stmt.executeQuery("select * from Class");
while(result.next()){
String name = result.getString("姓名");
Date Birthday= result.getDate("生日");
int phone = result.getInt("电话");
}
con.close();
}catch(Exception e){
System.out.println(e.toString());
}
|
直接连接,不要用odbc
try
{
// Driver
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
// Connect to db
String dbinfo = "jdbc:microsoft:sqlserver://yourserver:1433;";
dbinfo += "DatabaseName=yourdatabase;User=sa;Password=sa;";
Connection conn = DriverManager.getConnection(dbinfo);
}
catch(Exception e)
{
;
}
try
{
// Driver
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
// Connect to db
String dbinfo = "jdbc:microsoft:sqlserver://yourserver:1433;";
dbinfo += "DatabaseName=yourdatabase;User=sa;Password=sa;";
Connection conn = DriverManager.getConnection(dbinfo);
}
catch(Exception e)
{
;
}
|
result=stat.executeQuery("select vToyName,vToyDescription,siToyQoh,siLowerAge,siUpperAge,siToyWeight from 数据库名.表名");