package models;
import java.sql.*;public class DBUtil {
private static Connection con = null;
private PreparedStatement pstmt = null;
private ResultSet rs = null;
public static Connection getCon()
{
try {
Class.forName(
"com.microsoft.jdbc.sqlserver.SQLServerDriver");
con = DriverManager.getConnection(
"jdbc:microsoft:sqlserver://servername:1433;databaseName=pubs",
"sa", "ok");
} catch (SQLException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
return con;
} public void getClose(ResultSet rs,PreparedStatement pstmt,Connection con)
{
try {
if (rs != null) {
rs.close();
}
if (pstmt != null) {
pstmt.close();
}
if (con != null) {
con.close();
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
class DBtest{
public static void main(String[] args)
{ Connection con=null;
PreparedStatement pstmt=null;
ResultSet rs=null; try {
DBUtil d = new DBUtil();
pstmt = con.prepareStatement("select * from UserInfo");
rs = pstmt.executeQuery();
if (rs.next()) {
System.out.println("true");
}else
{
System.out.println("false");
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
import java.sql.*;public class DBUtil {
private static Connection con = null;
private PreparedStatement pstmt = null;
private ResultSet rs = null;
public static Connection getCon()
{
try {
Class.forName(
"com.microsoft.jdbc.sqlserver.SQLServerDriver");
con = DriverManager.getConnection(
"jdbc:microsoft:sqlserver://servername:1433;databaseName=pubs",
"sa", "ok");
} catch (SQLException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
return con;
} public void getClose(ResultSet rs,PreparedStatement pstmt,Connection con)
{
try {
if (rs != null) {
rs.close();
}
if (pstmt != null) {
pstmt.close();
}
if (con != null) {
con.close();
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
class DBtest{
public static void main(String[] args)
{ Connection con=null;
PreparedStatement pstmt=null;
ResultSet rs=null; try {
DBUtil d = new DBUtil();
pstmt = con.prepareStatement("select * from UserInfo");
rs = pstmt.executeQuery();
if (rs.next()) {
System.out.println("true");
}else
{
System.out.println("false");
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
结果是致命的错误不是代码产生的,看下IDE是不是有问题