我是Java编程的新手,我目前正在使用MVC架构开发我的第一个Web应用程序。但我的应用程序遇到了很多问题,我不知道它们为什么会出现。例如,我在JSP页面中有以下代码:
Connection connection = null;
PreparedStatement statement = null;
ResultSet rs = null;
String QueryString="";
String stuid="";
try {
QueryString = "SELECT Student_ID, Student_F_Name, Student_L_Name, Student_NIC, Student_Address, Student_DOB, Student_Email, Student_Gender, Course_Name, Batch_Name, Student_username, Student_password FROM Student WHERE Student_ID = ?";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
connection = DriverManager.getConnection("Jdbc:Odbc:UniversityDSN");
statement=connection.prepareStatement(QueryString);
stuid = session.getAttribute("studentid").toString();
statement.setString(1, stuid);
rs = statement.executeQuery();
有时这段代码完美无缺,但过了一段时间,此代码停止工作,并提供错误:
INFO:SQLException:[Microsoft] [ODBC驱动程序管理器]无效的字符串或缓冲区长度
但如果我修改上面的代码如下:
String QueryString = "SELECT Student_ID, Student_F_Name, Student_L_Name, Student_NIC, Student_Address, Student_DOB, Student_Email, Student_Gender, Course_Name, Batch_Name, Student_username, Student_password FROM Student WHERE Student_ID = ?";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
Connection connection = DriverManager.getConnection("Jdbc:Odbc:UniversityDSN");
PreparedStatement statement=connection.prepareStatement(QueryString);
String stuid = session.getAttribute("studentid").toString();
statement.setString(1, stuid);
ResultSet rs = statement.executeQuery();
它将重新开始工作。但有一段时间,这段代码将再次停止工作,给出相同的异常,我必须按照之前的方式修改它。 但是在这两次中,如果我调试代码,代码就能完美运行。 这非常烦人,因为我不得不浪费大量时间来修改我的代码,它发生在JSP Pages,Servlets或我使用的Bean类中。 如果有人可以告诉我我可能做错了什么以及如何避免这个问题,我真的很感激?
答案 0 :(得分:1)
首先,java odbc驱动程序对于Web服务器和多线程环境不是很好。我真的认为这与你的bug有关。
它丢失了数据库连接引用。你无能为力。唯一的方法是使用MS SQL的JDBC驱动程序,这是一个更好的Web环境驱动程序。
INFO:SQLException:[Microsoft] [ODBC驱动程序管理器]无效的字符串或缓冲区长度
它有时也与数据库服务器的升级有关 因为java odbc驱动程序的版本必须与db server版本匹配...
答案 1 :(得分:0)
在使用oracle数据库的外部连接器时,我遇到了这个问题。这里的问题是ODBC驱动程序。我使用的是Oracle 11G,并在源代码中创建了一个DSN。 Oracle 11G所需的JDBC-ODBC驱动程序是ojdbc5.jar。我改变了驱动程序,错误消失了。此错误主要与64位进程有关。