应用javabean无法连接数据库

使用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;

}

JSP部分

<%@ 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);
}

Exception的提示是
java.lang.NullPointerException
------最佳解决方案--------------------
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();
        }