使用javabean无法连接数据库
本帖最后由 poisson0106 于 2012-11-17 22:21:37 编辑 如题,已经试过在jsp下可以连接,但引用javabean就是不行,大家帮我看看,先谢谢啦~
java部分
package pratice.web;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class ContectSQL{
public static Connection getConnection() throws SQLException{
try {
Class.forName(driverName).newInstance();
conn=DriverManager.getConnection(url,user,password);
}
catch (Exception e) {
e.printStackTrace();
}
return conn;
}
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="";
private static Connection conn=null;
}
<%@ page language="java" contentType="text/html; charset=utf-8" import="java.sql.*"%>
<jsp:useBean id="ConString" scope="page" class="pratice.web.ContectSQL"></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 conn=ConString.getConnection();
PreparedStatement stmt=conn.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, "山东");
stmt.setString(5, "123456");
stmt.setString(6, "abc@gmail.com");
stmt.executeUpdate();
stmt.close();
}
catch(Exception e)
{
out.println(e);
}
}
}
[其他解释]
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;
}
没测过,你看这样行不行.
[其他解释]
不建议java代码都写在jsp中,日后很难维护,
out.println(e); 改成e.printStackTrace();
看看究竟是哪个为null
[其他解释]
我不大明白为什么try部分都过了但连接没有建成,请大神帮帮忙!!
[其他解释]
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;
}
}
<%@ 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, "abc@gmail.com");
stmt.executeUpdate();
stmt.close();
}
catch(SQLException e) { out.print(e); }
%>
</body>
</html>