java中MySQL与驱动jdbc连接不上,想请教高手:jdbc驱动:mysql-connector-java-5.1.29-bin.jar怎么用啊?
packagenet.chat;importjava.sql.*;importjava.lang.ClassNotFoundException;publicclassBa...
package net.chat;
import java.sql.*;
import java.lang.ClassNotFoundException;
public class BaseConn {
private Connection conn = null;
private Statement stmt = null;
private PreparedStatement ps = null;
private ResultSet rs = null;
public BaseConn() throws SQLException,ClassNotFoundException{
try
{
String driverName ="com.mysql.jdbc.Driver";
String userName = "root";
String passWord = "123";
String url="jdbc:mysql://localhost:3306/ChatRoom";
Class.forName(driverName);
Connection conn = DriverManager.getConnection(url,userName,passWord);
return ;
}
catch(SQLException e)
{
System.out.println("Error occured when Connect DataBase:"+e);
throw e;
}
catch(ClassNotFoundException e)
{
System.out.println("Error occured when Connect DataBase:"+e);
throw e;
}
}
public PreparedStatement preparedStatement(String sql) throws SQLException
{
try
{
ps = conn.prepareStatement(sql);
return ps;
}catch(SQLException e)
{
System.out.println("Error occured when create preparedStatement:"+e);
throw e;
}
}
public ResultSet executeQuery(String sql) throws SQLException
{
rs = null;
try {
rs = stmt.executeQuery(sql);
}
catch (SQLException ex) {
System.out.println("Error occured when query database:" + ex);
throw ex;
}
return rs;
}
public int executeUpdate(String sql) throws SQLException
{
try {
conn.setAutoCommit(false);
int re = stmt.executeUpdate(sql);
conn.commit();
return re;
}
catch (SQLException e) {
conn.rollback();
System.out.println("Error occured when update database:" + e);
throw e;
}
}
public ResultSet executeQuery() throws SQLException
{
try {
return ps.executeQuery();
}
catch (SQLException e) {
System.out.println("Error occured when query database:" + e);
throw e;
}
}
public int executeUpdate() throws SQLException
{
try {
conn.setAutoCommit(false);
int r = ps.executeUpdate();
conn.commit();
return r;
}
catch (SQLException e) {
conn.rollback();
System.out.println("Error occured when update database:" + e);
throw e;
}
}
public boolean closeDB() throws SQLException
{
try {
if (this.rs != null)
rs.close();
if (this.stmt != null)
this.stmt.close();
if (this.ps != null)
this.ps.close();
if (this.conn != null)
conn.close();
return true;
}
catch (SQLException e) {
System.out.println("Error occured when close database:" + e);
throw e;
}
}
}
求解,真的不懂,谢谢!
org.apache.jasper.JasperException: java.lang.NullPointerException
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:5
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:470)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
java.lang.NullPointerException
net.chat.BaseConn.preparedStatement(BaseConn.java:43)
net.chat.CheckLogin.checklogin(CheckLogin.java:33)
org.apache.jsp.chklogin_jsp._jspService(chklogin_jsp.java:100) 展开
import java.sql.*;
import java.lang.ClassNotFoundException;
public class BaseConn {
private Connection conn = null;
private Statement stmt = null;
private PreparedStatement ps = null;
private ResultSet rs = null;
public BaseConn() throws SQLException,ClassNotFoundException{
try
{
String driverName ="com.mysql.jdbc.Driver";
String userName = "root";
String passWord = "123";
String url="jdbc:mysql://localhost:3306/ChatRoom";
Class.forName(driverName);
Connection conn = DriverManager.getConnection(url,userName,passWord);
return ;
}
catch(SQLException e)
{
System.out.println("Error occured when Connect DataBase:"+e);
throw e;
}
catch(ClassNotFoundException e)
{
System.out.println("Error occured when Connect DataBase:"+e);
throw e;
}
}
public PreparedStatement preparedStatement(String sql) throws SQLException
{
try
{
ps = conn.prepareStatement(sql);
return ps;
}catch(SQLException e)
{
System.out.println("Error occured when create preparedStatement:"+e);
throw e;
}
}
public ResultSet executeQuery(String sql) throws SQLException
{
rs = null;
try {
rs = stmt.executeQuery(sql);
}
catch (SQLException ex) {
System.out.println("Error occured when query database:" + ex);
throw ex;
}
return rs;
}
public int executeUpdate(String sql) throws SQLException
{
try {
conn.setAutoCommit(false);
int re = stmt.executeUpdate(sql);
conn.commit();
return re;
}
catch (SQLException e) {
conn.rollback();
System.out.println("Error occured when update database:" + e);
throw e;
}
}
public ResultSet executeQuery() throws SQLException
{
try {
return ps.executeQuery();
}
catch (SQLException e) {
System.out.println("Error occured when query database:" + e);
throw e;
}
}
public int executeUpdate() throws SQLException
{
try {
conn.setAutoCommit(false);
int r = ps.executeUpdate();
conn.commit();
return r;
}
catch (SQLException e) {
conn.rollback();
System.out.println("Error occured when update database:" + e);
throw e;
}
}
public boolean closeDB() throws SQLException
{
try {
if (this.rs != null)
rs.close();
if (this.stmt != null)
this.stmt.close();
if (this.ps != null)
this.ps.close();
if (this.conn != null)
conn.close();
return true;
}
catch (SQLException e) {
System.out.println("Error occured when close database:" + e);
throw e;
}
}
}
求解,真的不懂,谢谢!
org.apache.jasper.JasperException: java.lang.NullPointerException
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:5
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:470)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
java.lang.NullPointerException
net.chat.BaseConn.preparedStatement(BaseConn.java:43)
net.chat.CheckLogin.checklogin(CheckLogin.java:33)
org.apache.jsp.chklogin_jsp._jspService(chklogin_jsp.java:100) 展开
1个回答
展开全部
jdbc驱动:mysql-connector-java-5.1.29-bin.jar怎么用啊?
你需要把这个jar包导入到项目里面,然后你就可以使用包里的类和函数了。
你需要把这个jar包导入到项目里面,然后你就可以使用包里的类和函数了。
更多追问追答
追问
已经放了,但是数据库接不上。原来这个程序是接SqlServer2005的,我的是MySQL,想改成MYSQL,但是不会改。。一直接不上数据库
已经放了,但是数据库接不上。原来这个程序是接SqlServer2005的,我的是MySQL,想改成MYSQL,但是不会改。。一直接不上数据库
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询