本帖最后由 poisson0106 于 2012-11-17 22:21:37 编辑

解决方案 »

  1.   

    我不大明白为什么try部分都过了但连接没有建成,请大神帮帮忙!!
      

  2.   

    不建议java代码都写在jsp中,日后很难维护,out.println(e); 改成e.printStackTrace();
    看看究竟是哪个为null
      

  3.   

    换成e.printStackTrace()之后连错误信息都不输出了,但调试中看得出是conn为空,就是没连上数据库好像。
      

  4.   

     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;
    }
    没测过,你看这样行不行.
      

  5.   

    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();
            }
        }      
    }
      

  6.   

    把你的 return语句放到try内  异常外return null 就可以了  
      

  7.   

    好像成功了,放上最新的代码,但是我觉得和我之前的没啥区别,能不能请大神指点下区别在哪里?想搞清楚哪里错了。
    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>
      

  8.   

    注册驱动括弧里直接写驱动名称:
    Class.forName()你看你写的 
    Class.forName(driverName).newInstance();
      

  9.   

    我是看http://blog.163.com/jackie_howe/blog/static/19949134720125173539380/这个帖子连接的,原来一定要分开的么?这里面driverName不会调用外面静态的那个定义么?