连接SQL数据库,一直连不上,显示不了数据。是代码有错误吗,还是什么配置有错误,急求帮助。下面是代码 10
packageclasssource;importjava.sql.*;publicclassDatabase{publicstaticConnectioncn;publ...
package classsource;
import java.sql.*;
public class Database {
public static Connection cn;
public static Statement st;
public static ResultSet rs;
public static boolean joinDB() {
boolean joinFlag;
try {
joinFlag = true;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
cn = DriverManager.getConnection("jdbc:odbc:EmployeeInformationMS","sa","");
cn.setCatalog("EmployeeInformationMS");
System.out.println("数据库连接成功");
st = cn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
return joinFlag;
} catch (SQLException sqlEx) {
System.out.println(sqlEx.getMessage());
joinFlag = false;
return joinFlag;
} catch (ClassNotFoundException notfoundEX) {
System.out.println(notfoundEX.getMessage());
joinFlag = false;
return joinFlag;
}
}
public static boolean executeSQL(String sqlString) {
boolean executeFlag;
try {
st.execute(sqlString);
executeFlag = true;
} catch (Exception e) {
executeFlag = false;
System.out.println("sql exception:" + e.getMessage());
}
return executeFlag;
}
public static boolean query(String sqlString) {
try {
rs = null;
//System.out.println(sqlString);
rs = st.executeQuery(sqlString);
} catch (Exception Ex) {
System.out.println("sql exception:" + Ex);
return false;
}
return true;
}
}
做查询操作时会输出sql exception:java.lang.NullPointerException 展开
import java.sql.*;
public class Database {
public static Connection cn;
public static Statement st;
public static ResultSet rs;
public static boolean joinDB() {
boolean joinFlag;
try {
joinFlag = true;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
cn = DriverManager.getConnection("jdbc:odbc:EmployeeInformationMS","sa","");
cn.setCatalog("EmployeeInformationMS");
System.out.println("数据库连接成功");
st = cn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
return joinFlag;
} catch (SQLException sqlEx) {
System.out.println(sqlEx.getMessage());
joinFlag = false;
return joinFlag;
} catch (ClassNotFoundException notfoundEX) {
System.out.println(notfoundEX.getMessage());
joinFlag = false;
return joinFlag;
}
}
public static boolean executeSQL(String sqlString) {
boolean executeFlag;
try {
st.execute(sqlString);
executeFlag = true;
} catch (Exception e) {
executeFlag = false;
System.out.println("sql exception:" + e.getMessage());
}
return executeFlag;
}
public static boolean query(String sqlString) {
try {
rs = null;
//System.out.println(sqlString);
rs = st.executeQuery(sqlString);
} catch (Exception Ex) {
System.out.println("sql exception:" + Ex);
return false;
}
return true;
}
}
做查询操作时会输出sql exception:java.lang.NullPointerException 展开
2016-06-30
展开全部
你没有说清楚是什么软件,如果软件需要连接远程数据库的话,如果远程服务器上面的sql没有启动,或者远程服务器运行不正常,都可能出现这个提示
如果连接是你本机的数据库,那你检查你本机数据库有没有启动,
如果连接是你本机的数据库,那你检查你本机数据库有没有启动,
追问
一个人员管理系统软件,用NetBean运行的。连接的是本地数据,错误会是出于代码吗
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询