帮我看看JAVA连接mysql的代码
前面一段是对的,可以用!!!!为什么后面一段我的就连不上,真气人啊!!importjava.sql.Connection;importjava.sql.DriverMan...
前面一段是对的,可以用!!!!为什么后面一段我的就连不上,真气人啊!!
import java.sql.Connection;
import java.sql.DriverManager;
public class ConnectionUtil {
private String driver="com.mysql.jdbc.Driver";
private String url = "jdbc:mysql://localhost:3306/333?user=root&password=test&useUnicode=true";
public Connection getConnection() {
try {
Class.forName(driver);
return DriverManager.getConnection(url);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
ConnectionUtil cu = new ConnectionUtil();
Connection conn = cu.getConnection();
System.out.println(conn);
}
}
我的代码
package cao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import org.apache.struts.action.Action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import chaoren.GoodsForm;
import java.sql.*;
public class GoodsAction extends Action {
GoodsForm goodsform=null;
private int categoreyId =0;
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
goodsform=new GoodsForm();
categoreyId=Integer.parseInt(request.getParameter("id"));
String dbDriver = "com.mysql.jdbc.Driver"; // 数据库的驱动
String url="jdbc:mysql://localhost:3306/333?user=root&password=test&useUnicode=true"; // URL地址
Connection connection = null;
try {
Class.forName(dbDriver); // 加载数据库驱动
connection = DriverManager.getConnection(url);// 加载数据库
} catch (Exception ex) {
System.out.println("数据库加载失败");
}
PreparedStatement ps = null;
ps = connection.prepareStatement("select max(ID)+1 as ID from dept where categoreyId="+categoreyId+"");
ResultSet rs=ps.executeQuery();
while(rs.next())
goodsform=new GoodsForm();
goodsform.setCode(rs.getString("ID"));
System.out.println(goodsform.getCode());
request.setAttribute("codes", goodsform);
return mapping.findForward("good");}
}
错误提示:
3
数据库加载失败
2009-11-5 11:54:43 org.apache.struts.action.RequestProcessor processException
警告: Unhandled Exception thrown: class java.lang.NullPointerException
2009-11-5 11:54:43 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet action threw exception
SQL语句是没有错误的,我在SQL编辑器上测试过了!!!
我也知道是空指针但是哪错了???具体些谢谢了 展开
import java.sql.Connection;
import java.sql.DriverManager;
public class ConnectionUtil {
private String driver="com.mysql.jdbc.Driver";
private String url = "jdbc:mysql://localhost:3306/333?user=root&password=test&useUnicode=true";
public Connection getConnection() {
try {
Class.forName(driver);
return DriverManager.getConnection(url);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
ConnectionUtil cu = new ConnectionUtil();
Connection conn = cu.getConnection();
System.out.println(conn);
}
}
我的代码
package cao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import org.apache.struts.action.Action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import chaoren.GoodsForm;
import java.sql.*;
public class GoodsAction extends Action {
GoodsForm goodsform=null;
private int categoreyId =0;
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
goodsform=new GoodsForm();
categoreyId=Integer.parseInt(request.getParameter("id"));
String dbDriver = "com.mysql.jdbc.Driver"; // 数据库的驱动
String url="jdbc:mysql://localhost:3306/333?user=root&password=test&useUnicode=true"; // URL地址
Connection connection = null;
try {
Class.forName(dbDriver); // 加载数据库驱动
connection = DriverManager.getConnection(url);// 加载数据库
} catch (Exception ex) {
System.out.println("数据库加载失败");
}
PreparedStatement ps = null;
ps = connection.prepareStatement("select max(ID)+1 as ID from dept where categoreyId="+categoreyId+"");
ResultSet rs=ps.executeQuery();
while(rs.next())
goodsform=new GoodsForm();
goodsform.setCode(rs.getString("ID"));
System.out.println(goodsform.getCode());
request.setAttribute("codes", goodsform);
return mapping.findForward("good");}
}
错误提示:
3
数据库加载失败
2009-11-5 11:54:43 org.apache.struts.action.RequestProcessor processException
警告: Unhandled Exception thrown: class java.lang.NullPointerException
2009-11-5 11:54:43 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet action threw exception
SQL语句是没有错误的,我在SQL编辑器上测试过了!!!
我也知道是空指针但是哪错了???具体些谢谢了 展开
6个回答
展开全部
connection = DriverManager.getConnection(url);// 加载数据库
这句不对。
应该是:DriverManager.getConnection(url,username,password);
url应该是url=jdbc:mysql://127.0.0.1:3306/21com_database这种格式 是你的数据库的地址/数据库的名字
这句不对。
应该是:DriverManager.getConnection(url,username,password);
url应该是url=jdbc:mysql://127.0.0.1:3306/21com_database这种格式 是你的数据库的地址/数据库的名字
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
检查一下你的SQL语句是否错误
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
82938641牛人请进,帮帮我们这些对JAVA又爱又恨的初学者。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
5楼你说的不对,getConnection(url)是可以的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2009-11-05
展开全部
是你的struts有问题吧?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询