package dbcon;
import java.sql.*;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.naming.*;
import javax.sql.*;
public class mydb {
String strUser= " ",strPassword= " ";
String strDbdriver= "sun.jdbc.odbc.JdbcOdbcDriver ";
String strConstring= "jdbc:odbc:ibbs ";
//String strDbdriver = "org.gjt.mm.mysql.Driver ";
//String strConstring = "jdbc:mysql://localhost:3306/ibbs ";
Connection conn = null;
ResultSet rs = null;
public mydb() {
try {//--read db info from config file-------------
Class.forName(strDbdriver);
}
catch(Exception e) {
System.err.println( "mydb(): " + e.getMessage());
}
}
//----query------------------
public ResultSet executeQuery(String sql)throws Exception {
rs = null;
conn = DriverManager.getConnection(strConstring,strUser,strPassword);
Statement stmt = conn.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(sql);
if(rs==null){System.exit(1);}
return rs;
}
//---Update-------------------
public void executeUpdate(String sql) {
try {
conn = DriverManager.getConnection(strConstring,strUser,strPassword);
Statement stmt = conn.createStatement(java.sql.ResultSet.TYPE_SCROLL_SENSITIVE,java.sql.ResultSet.CONCUR_UPDATABLE);
stmt.executeUpdate(sql);
stmt.close();
conn.close();
}
catch(SQLException ex) {
System.err.println( "aq.executeUpdate: " + ex.getMessage());
}
}
}
提示错误如下:
javax.servlet.ServletException: [Microsoft][ODBC 驱动程序管理器] 未发现数据源名称并且未指定默认驱动程序
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:825)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:758)
org.apache.jsp.regnew_jsp._jspService(regnew_jsp.java:107)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
root cause
java.sql.SQLException: [Microsoft][ODBC 驱动程序管理器] 未发现数据源名称并且未指定默认驱动程序
sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6958)
sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7115)
sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(JdbcOdbc.java:3074)
sun.jdbc.odbc.JdbcOdbcConnection.initialize(JdbcOdbcConnection.java:323)
sun.jdbc.odbc.JdbcOdbcDriver.connect(JdbcOdbcDriver.java:174)
java.sql.DriverManager.getConnection(DriverManager.java:512)
java.sql.DriverManager.getConnection(DriverManager.java:171)
dbcon.mydb.executeQuery(mydb.java:35)