当前位置: 技术问答>java相关
怎么就是不能通过class链接数据库??我试了整天了。
来源: 互联网 发布时间:2017-04-26
本文导语: 我用jsp链接mysql数据库是可以,代码如下: -------------------------------- ------------------------------------- 但是如果我先是写个class,然后用jsp链接就说错误, connectDB...
我用jsp链接mysql数据库是可以,代码如下:
--------------------------------
-------------------------------------
但是如果我先是写个class,然后用jsp链接就说错误,
connectDB.java:
-------------------------------------
package DB;
import java.sql.*;
import java.io.*;
public class connectDB
{
String sDBDriver="org.gjt.mm.mysql.Driver";
String DBname="dbase";
String sUrl="jdbc:mysql://localhost/"+DBname;
String DBusername="kafe";
String DBpassword="81yjuly05d";
Connection conn=null;
public void setDBname(String DBname)
{
this.DBname=DBname;
}
public void connectDB()
{
try
{
Class.forName(sDBDriver);
}
catch(java.lang.ClassNotFoundException e)
{
System.out.println("It can't connect the Datebase!!!");
}
}
public ResultSet queryDB(String sSql)
{
ResultSet RS=null;
try
{
conn=DriverManager.getConnection(sUrl,DBusername,DBpassword);
Statement stmt=conn.createStatement();
RS=stmt.executeQuery(sSql);
stmt.close();
conn.close();
}
catch(SQLException ex)
{
System.out.println("It can't run this SQL:"+sSql);
}
return RS;
}
}
------------------------------------
利用connectDB.class链接的jsp文件如下:
总是报下面的错误:
java.lang.NullPointerException
at _jsp._message._testmessage2__jsp._jspService(/jsp/message/testmessage2.jsp:15)
at com.caucho.jsp.JavaPage.service(JavaPage.java:74)
at com.caucho.jsp.Page.subservice(Page.java:485)
at com.caucho.server.http.FilterChainPage.doFilter(FilterChainPage.java:181)
at com.caucho.server.http.Invocation.service(Invocation.java:291)
at com.caucho.server.http.CacheInvocation.service(CacheInvocation.java:132)
at com.caucho.server.http.HttpRequest.handleRequest(HttpRequest.java:217)
at com.caucho.server.http.HttpRequest.handleConnection(HttpRequest.java:159)
at com.caucho.server.TcpConnection.run(TcpConnection.java:136)
at java.lang.Thread.run(Thread.java:536)
我查了好多地方了,都觉得没有错啊,是不是我的resin有问题啊??冥想了好久好久。。。
--------------------------------
-------------------------------------
但是如果我先是写个class,然后用jsp链接就说错误,
connectDB.java:
-------------------------------------
package DB;
import java.sql.*;
import java.io.*;
public class connectDB
{
String sDBDriver="org.gjt.mm.mysql.Driver";
String DBname="dbase";
String sUrl="jdbc:mysql://localhost/"+DBname;
String DBusername="kafe";
String DBpassword="81yjuly05d";
Connection conn=null;
public void setDBname(String DBname)
{
this.DBname=DBname;
}
public void connectDB()
{
try
{
Class.forName(sDBDriver);
}
catch(java.lang.ClassNotFoundException e)
{
System.out.println("It can't connect the Datebase!!!");
}
}
public ResultSet queryDB(String sSql)
{
ResultSet RS=null;
try
{
conn=DriverManager.getConnection(sUrl,DBusername,DBpassword);
Statement stmt=conn.createStatement();
RS=stmt.executeQuery(sSql);
stmt.close();
conn.close();
}
catch(SQLException ex)
{
System.out.println("It can't run this SQL:"+sSql);
}
return RS;
}
}
------------------------------------
利用connectDB.class链接的jsp文件如下:
总是报下面的错误:
java.lang.NullPointerException
at _jsp._message._testmessage2__jsp._jspService(/jsp/message/testmessage2.jsp:15)
at com.caucho.jsp.JavaPage.service(JavaPage.java:74)
at com.caucho.jsp.Page.subservice(Page.java:485)
at com.caucho.server.http.FilterChainPage.doFilter(FilterChainPage.java:181)
at com.caucho.server.http.Invocation.service(Invocation.java:291)
at com.caucho.server.http.CacheInvocation.service(CacheInvocation.java:132)
at com.caucho.server.http.HttpRequest.handleRequest(HttpRequest.java:217)
at com.caucho.server.http.HttpRequest.handleConnection(HttpRequest.java:159)
at com.caucho.server.TcpConnection.run(TcpConnection.java:136)
at java.lang.Thread.run(Thread.java:536)
我查了好多地方了,都觉得没有错啊,是不是我的resin有问题啊??冥想了好久好久。。。
|
String sUrl="jdbc:mysql://localhost/dbase";
是不是缺端口号,如用ms sqlserver2000,端口号是1433,mysql就不知道了。
是不是缺端口号,如用ms sqlserver2000,端口号是1433,mysql就不知道了。
|
你的数据库不会是连好几个吧,干嘛要写得这么麻烦?
还有,你的方法名还和类名一样.
改成下面这样的再试试.
package DB;
import java.sql.*;
import java.io.*;
public class connectDB
{
String sDBDriver="org.gjt.mm.mysql.Driver";
String DBname="dbase";
String sUrl="jdbc:mysql://localhost/dbase";
String DBusername="kafe";
String DBpassword="81yjuly05d";
Connection conn=null;
public connectDB()
{
try
{
Class.forName(sDBDriver);
conn=DriverManager.getConnection(sUrl,DBusername,DBpassword);
}
catch(java.lang.ClassNotFoundException e)
{
System.out.println("It can't connect the Datebase!!!");
}
}
public ResultSet queryDB(String sSql)
{
ResultSet RS=null;
try
{
Statement stmt=conn.createStatement();
RS=stmt.executeQuery(sSql);
stmt.close();
conn.close();
}
catch(SQLException ex)
{
System.out.println("It can't run this SQL:"+sSql);
}
return RS;
}
}
还有,你的方法名还和类名一样.
改成下面这样的再试试.
package DB;
import java.sql.*;
import java.io.*;
public class connectDB
{
String sDBDriver="org.gjt.mm.mysql.Driver";
String DBname="dbase";
String sUrl="jdbc:mysql://localhost/dbase";
String DBusername="kafe";
String DBpassword="81yjuly05d";
Connection conn=null;
public connectDB()
{
try
{
Class.forName(sDBDriver);
conn=DriverManager.getConnection(sUrl,DBusername,DBpassword);
}
catch(java.lang.ClassNotFoundException e)
{
System.out.println("It can't connect the Datebase!!!");
}
}
public ResultSet queryDB(String sSql)
{
ResultSet RS=null;
try
{
Statement stmt=conn.createStatement();
RS=stmt.executeQuery(sSql);
stmt.close();
conn.close();
}
catch(SQLException ex)
{
System.out.println("It can't run this SQL:"+sSql);
}
return RS;
}
}
|
试着用useBean来看看。?