连接管理文件抛出异常

时间:2016-07-06 10:16:59

标签: java jdbc classnotfoundexception

代码-1 :(正常工作)

package com.connection;
import java.sql.*;

public class SqlServerJDBC {

public static void main(String[] args) {

    try
    {
        String server,user,password,database;
        int port;
        String jdbcUrl;

        server="admin-PC";
        port =1433;
        user="sa";
        password="Admin@123";
        database="ORDD_LOGIN_DB";

        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        System.out.println("#- driver load");
        jdbcUrl="jdbc:sqlserver://"+server+":"+port+";user="+user+";password="+password+";databaseName="+database+";";
        System.out.println("CONstring="+jdbcUrl); 

        Connection con=DriverManager.getConnection(jdbcUrl);
        System.out.println("# - CONNECTION OBTAINED");


        Statement stmt=con.createStatement();
        System.out.println("# - STATMENT OBTAINED");

        ResultSet rs=stmt.executeQuery("select * from UserInfo;");
        System.out.println("# - query executed");

        if (rs.next())
        {
            System.out.println("output colume = "+rs.getString(1));
        }

        rs.close();stmt.close();con.close(); System.out.println("resource relised......done");



    }catch(Exception e)
    {
        System.out.print(e);
    }
}

}

代码2 (不工作)

package com.connection;
import java.sql.*;

public class SqlServerJDBC {

public static Connection getconnection() {
    Connection con = null;

    try
    {
        String server,user,password,database,jdbcUrl;
        int port;
        server="admin-PC";port =1433;user="sa";password="Admin@123";database="ORDD_LOGIN_DB";

        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        System.out.println("#- driver load");

        jdbcUrl="jdbc:sqlserver://"+server+":"+port+";user="+user+";password="+password+";databaseName="+database+";";
        System.out.println("CONstring="+jdbcUrl); 

         con=DriverManager.getConnection(jdbcUrl);
        System.out.println("# - CONNECTION OBTAINED");



    }catch(Exception e)
    {
        System.out.print(e);
    }
    System.out.println(con);
    return con;
}
} 

错误按摩:

java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver null java.lang.NullPointerException

0 个答案:

没有答案