当前位置: 技术问答>java相关
jdbc连接问题
来源: 互联网 发布时间:2015-07-28
本文导语: import java.sql.*; class Jdbctest { public static void main(String[] args) { try { Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); } catch(java.lang.ClassNotFoundException e) { System.err.print("ClassNotFoundException: "); System.err.println(e...
import java.sql.*;
class Jdbctest
{
public static void main(String[] args)
{
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
} catch(java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}
String url="jdbc:microsoft:sqlserver://ag-server:1433;DatabaseName=pubs";
String user="yingps";
String password="";
try{
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from test";
ResultSet rs=stmt.executeQuery(sql);
System.out.print("No. ");
System.out.println("Name");
while(rs.next()) {
System.out.print(rs.getString(1));
System.out.print(" ");
System.out.println(rs.getString(2));
}
System.out.println("数据库操作成功,恭喜你");
rs.close();
stmt.close();
conn.close();
}catch(SQLException ex) {
System.err.println("SQLException: " + ex.getMessage());
//显示数据库连接错误或者查询错误
}
}
}
上面这种连接方式在jdk环境下是能成功的。
但放到tomcat下用jsp连接却报no suitable driver com.microsoft.jdbc.sqlserver.SQLServerDriver
请各位帮忙啊
class Jdbctest
{
public static void main(String[] args)
{
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
} catch(java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}
String url="jdbc:microsoft:sqlserver://ag-server:1433;DatabaseName=pubs";
String user="yingps";
String password="";
try{
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from test";
ResultSet rs=stmt.executeQuery(sql);
System.out.print("No. ");
System.out.println("Name");
while(rs.next()) {
System.out.print(rs.getString(1));
System.out.print(" ");
System.out.println(rs.getString(2));
}
System.out.println("数据库操作成功,恭喜你");
rs.close();
stmt.close();
conn.close();
}catch(SQLException ex) {
System.err.println("SQLException: " + ex.getMessage());
//显示数据库连接错误或者查询错误
}
}
}
上面这种连接方式在jdk环境下是能成功的。
但放到tomcat下用jsp连接却报no suitable driver com.microsoft.jdbc.sqlserver.SQLServerDriver
请各位帮忙啊
|
如果有.jar驱动包,请放到web-inf/lib中。或common/lib中。