statement报空指针
新手还望大家帮助packageutil;importjava.sql.Connection;importjava.sql.DriverManager;importjava...
新手还望大家帮助
package util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class ServerConn {
private String driver = "com.mysql.jdbc.Driver";
private String url = "jdbc:mysql://localhost:3306/mysql";
private String username = "root";
private String password = "root";
private static Connection conn;
@SuppressWarnings("finally")
public Connection serverConn() {
try {
Class.forName(driver);
try {
System.out.println("aaa");
conn = DriverManager.getConnection(url, username, password);
} catch (SQLException e) {
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}finally{
if (conn!=null) {
return conn;
}else{
return null;
}
}
}
public ResultSet modle1(){
String sqlStr = "select * from mysql.db";
Statement state = null;
ResultSet rs = null;
try {
System.out.println("aa");
state = conn.createStatement();
System.out.println("bb");
rs = state.executeQuery(sqlStr);
System.out.println("cc");
if(rs.next()){
System.out.println(rs.getInt(1));
System.out.println(rs.getString(2));
}
state.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
public void close(Connection conn){
if (conn!=null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
System.out.println(new ServerConn().modle1());
new ServerConn().close(conn);
}
}
报错:
aa
Exception in thread "main" java.lang.NullPointerException
at util.ServerConn.modle1(ServerConn.java:43)
at util.ServerConn.main(ServerConn.java:68)
补充数据库连接没问题conn也能得到 展开
package util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class ServerConn {
private String driver = "com.mysql.jdbc.Driver";
private String url = "jdbc:mysql://localhost:3306/mysql";
private String username = "root";
private String password = "root";
private static Connection conn;
@SuppressWarnings("finally")
public Connection serverConn() {
try {
Class.forName(driver);
try {
System.out.println("aaa");
conn = DriverManager.getConnection(url, username, password);
} catch (SQLException e) {
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}finally{
if (conn!=null) {
return conn;
}else{
return null;
}
}
}
public ResultSet modle1(){
String sqlStr = "select * from mysql.db";
Statement state = null;
ResultSet rs = null;
try {
System.out.println("aa");
state = conn.createStatement();
System.out.println("bb");
rs = state.executeQuery(sqlStr);
System.out.println("cc");
if(rs.next()){
System.out.println(rs.getInt(1));
System.out.println(rs.getString(2));
}
state.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
public void close(Connection conn){
if (conn!=null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
System.out.println(new ServerConn().modle1());
new ServerConn().close(conn);
}
}
报错:
aa
Exception in thread "main" java.lang.NullPointerException
at util.ServerConn.modle1(ServerConn.java:43)
at util.ServerConn.main(ServerConn.java:68)
补充数据库连接没问题conn也能得到 展开
展开全部
很久没用Java连接MySQL了,但看了下,你的类名是ServerConn ,构造函数却是serverConn (第一个字母小写,而且构造函数不能有Connection类型的返回值), 因此编译器提供了一个塌闹空实现的构造函数,ServerConn(),在Main里面new的时候却没有调用到你的serverConn,导致conn这个变量没有被初始化。
也或者你本身就是要调用serverConn,这样的话,应该要首橘稿先调用一次才对。
ServerConn test = new ServerConn();
test.serverConn();//这圆衫孝里才开始初始化conn连接
test.model1();
也或者你本身就是要调用serverConn,这样的话,应该要首橘稿先调用一次才对。
ServerConn test = new ServerConn();
test.serverConn();//这圆衫孝里才开始初始化conn连接
test.model1();
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询