使用javabean无法连接数据库 本帖最后由 poisson0106 于 2012-11-17 22:21:37 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我不大明白为什么try部分都过了但连接没有建成,请大神帮帮忙!! 不建议java代码都写在jsp中,日后很难维护,out.println(e); 改成e.printStackTrace();看看究竟是哪个为null 换成e.printStackTrace()之后连错误信息都不输出了,但调试中看得出是conn为空,就是没连上数据库好像。 public ContectSQL() throws Exception{ try { Class.forName(driverName).newInstance(); conn=DriverManager.getConnection(url,user,password); } catch (Exception e) { e.printStackTrace(); } } public static synchronized Connection getConn() { try { instance = new ContectSQL(); } catch (ConnectionException ex) { instance=null; } return instance.conn; }没测过,你看这样行不行. package pratice.web; import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException; public class ContectSQL{ private static String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver"; private static String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=Pratice"; private static String user="sa"; private static String password=""; public static void getConnection() throws SQLException{ try { Class.forName(driverName); conn=DriverManager.getConnection(url,user,password); } catch (Exception e) { e.printStackTrace(); } } } 把你的 return语句放到try内 异常外return null 就可以了 好像成功了,放上最新的代码,但是我觉得和我之前的没啥区别,能不能请大神指点下区别在哪里?想搞清楚哪里错了。javabean部分package pratice.web;import java.sql.Connection;import java.sql.DriverManager;public class MethodOfSQL {public static Connection GetConnection(){ Connection con = null; try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); } catch(Exception e) { e.printStackTrace(); } try { con=DriverManager.getConnection("jdbc:sqlserver://localhost:1433; DatabaseName= Pratice","sa",""); } catch(Exception e) { e.printStackTrace(); } return con;}}jsp部分<%@ page language="java" contentType="text/html; charset=utf-8" import="java.sql.*"%><jsp:useBean id="Constring" scope="page" class="pratice.web.MethodOfSQL"></jsp:useBean><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>Insert title here</title></head><body><%try { Connection con=Constring.GetConnection(); PreparedStatement stmt=con.prepareStatement("insert into UserInfo(UserID,UserName,Age,Address,Phone,Email) values(?,?,?,?,?,?)"); stmt.setInt(1, 6); stmt.setString(2, "abc"); stmt.setInt(3, 21); stmt.setString(4, "abc"); stmt.setString(5, "123456"); stmt.setString(6, "[email protected]"); stmt.executeUpdate(); stmt.close();}catch(SQLException e) { out.print(e); } %></body></html> 注册驱动括弧里直接写驱动名称:Class.forName()你看你写的 Class.forName(driverName).newInstance(); 我是看http://blog.163.com/jackie_howe/blog/static/19949134720125173539380/这个帖子连接的,原来一定要分开的么?这里面driverName不会调用外面静态的那个定义么? <img>被删除 滚动又出来了 如何验证一组radiobutton必须有选中项 struts 上传图片并生成缩略图如何处理gif 高手进共同探讨! 怎样用java实现分页显示 JSP帮助文档(急) 请问如何读取SQL2000中text类型的数据! ★★★★★★★关于在网页中使用控件表格的问题,急!!! 一般正式java网站注册邮箱验证,激活怎么做 关于tomcat的问题??? 深夜询问ajax返回json数据问题 怎样取多个隐藏表单域中的值并便利出来 如何利用jspsmartupload实现批量上传文件
看看究竟是哪个为null
try {
Class.forName(driverName).newInstance();
conn=DriverManager.getConnection(url,user,password);
}
catch (Exception e) {
e.printStackTrace();
}
}
public static synchronized Connection getConn() {
try {
instance = new ContectSQL();
} catch (ConnectionException ex) {
instance=null;
}
return instance.conn;
}
没测过,你看这样行不行.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ContectSQL{
private static String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=Pratice";
private static String user="sa";
private static String password=""; public static void getConnection() throws SQLException{
try {
Class.forName(driverName);
conn=DriverManager.getConnection(url,user,password);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
javabean部分package pratice.web;import java.sql.Connection;
import java.sql.DriverManager;public class MethodOfSQL {
public static Connection GetConnection(){
Connection con = null;
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
}
catch(Exception e)
{
e.printStackTrace();
}
try
{
con=DriverManager.getConnection("jdbc:sqlserver://localhost:1433; DatabaseName= Pratice","sa","");
}
catch(Exception e)
{
e.printStackTrace();
}
return con;
}
}jsp部分<%@ page language="java" contentType="text/html; charset=utf-8" import="java.sql.*"%>
<jsp:useBean id="Constring" scope="page" class="pratice.web.MethodOfSQL"></jsp:useBean>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<%
try
{ Connection con=Constring.GetConnection();
PreparedStatement stmt=con.prepareStatement("insert into UserInfo(UserID,UserName,Age,Address,Phone,Email) values(?,?,?,?,?,?)");
stmt.setInt(1, 6);
stmt.setString(2, "abc");
stmt.setInt(3, 21);
stmt.setString(4, "abc");
stmt.setString(5, "123456");
stmt.setString(6, "[email protected]");
stmt.executeUpdate();
stmt.close();
}
catch(SQLException e) { out.print(e); }
%>
</body>
</html>
Class.forName()你看你写的
Class.forName(driverName).newInstance();