主题:救助:javabean如何连接MYSQL
刚刚学习javabean,恳求各位大侠帮忙看看。这样写对么?
dbconn.java:
package Border;
import java.sql.*;
public class dbconn {
public dbconn(){
}
private Connection conn = null;
private String server = "127.0.0.1";
private String port = "3306";
private String db = "publish";
private String user = "root";
private String pass = "root";
private String drivername="org.gjt.mm.mysql.Driver";
private String URL="jdbc:mysql://"+server+":"+port+"/"+db+"?useUnicode=true&characterEncoding=GBK&user="+user+"&password="+pass;
public Connection getConn(){
try{
Class.forName(drivername).newInstance();
conn = DriverManager.getConnection(URL);
}
catch(Exception e){
e.printStackTrace();
}
return conn ;
}
}
test.jsp:
<%@ page language="java" import="java.util.*" import="java.sql.*" pageEncoding="gb2312"%>
<jsp:useBean id="Border" scope="page" class="Border.dbconn" />
<%boolean f=false; %>
<%Connection conn = null;
conn = Border.getConn() ;
%>
<%String sql="select * from book";
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()){
out.println(rs.getInt(1));
out.print(rs.getString(2));
out.print(rs.getString(3));
}
%>
<html>
<head>
</head>
<body>
</body>
</html>
错误:
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: An exception occurred processing JSP page /ss.jsp at line 8
5: conn = Border.getConn() ;
6: %>
7: <%String sql="select * from book";
8: Statement stmt=conn.createStatement();
9: ResultSet rs=stmt.executeQuery(sql);
10: while(rs.next()){
11: out.println(rs.getInt(1));
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:510)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause
java.lang.NullPointerException
org.apache.jsp.ss_jsp._jspService(ss_jsp.java:76)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
dbconn.java:
package Border;
import java.sql.*;
public class dbconn {
public dbconn(){
}
private Connection conn = null;
private String server = "127.0.0.1";
private String port = "3306";
private String db = "publish";
private String user = "root";
private String pass = "root";
private String drivername="org.gjt.mm.mysql.Driver";
private String URL="jdbc:mysql://"+server+":"+port+"/"+db+"?useUnicode=true&characterEncoding=GBK&user="+user+"&password="+pass;
public Connection getConn(){
try{
Class.forName(drivername).newInstance();
conn = DriverManager.getConnection(URL);
}
catch(Exception e){
e.printStackTrace();
}
return conn ;
}
}
test.jsp:
<%@ page language="java" import="java.util.*" import="java.sql.*" pageEncoding="gb2312"%>
<jsp:useBean id="Border" scope="page" class="Border.dbconn" />
<%boolean f=false; %>
<%Connection conn = null;
conn = Border.getConn() ;
%>
<%String sql="select * from book";
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()){
out.println(rs.getInt(1));
out.print(rs.getString(2));
out.print(rs.getString(3));
}
%>
<html>
<head>
</head>
<body>
</body>
</html>
错误:
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: An exception occurred processing JSP page /ss.jsp at line 8
5: conn = Border.getConn() ;
6: %>
7: <%String sql="select * from book";
8: Statement stmt=conn.createStatement();
9: ResultSet rs=stmt.executeQuery(sql);
10: while(rs.next()){
11: out.println(rs.getInt(1));
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:510)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause
java.lang.NullPointerException
org.apache.jsp.ss_jsp._jspService(ss_jsp.java:76)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)