34行处PreparedStatement ps = conn.prepareStatement(sql);空指针异常。小白请大神指正一下错误的地方。 6
packageutil;importjava.sql.*;publicclassDBconn{staticStringurl="jdbc:mysql://localhos...
package util;
import java.sql.*;
public class DBconn {
static String url = "jdbc:mysql://localhost:3306/test?useunicode=true& characterEncoding=utf8";
static String username = "root";
static String password = "1234";
static Connection conn = null;
static ResultSet rs = null;
static PreparedStatement ps = null;
public static void init() {
if( conn == null){
try {
Class.forName("mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, username, password);
} catch (Exception e) {
System.out.println("init [SQL驱动程序初始化失败!]");
e.printStackTrace();
}
try {
Class.forName("mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, username, password);
} catch (Exception e) {
System.out.println("init [SQL驱动程序初始化失败!]");
e.printStackTrace();
}
}
}
public static int addUpdDel(String sql) {
int i = 0;
try {
PreparedStatement ps = conn.prepareStatement(sql);
i = ps.executeUpdate();
} catch (SQLException e) {
System.out.println("sql数据库增删改异常");
e.printStackTrace();
}
return i;
}
public static ResultSet selectSql(String sql) {
try {
ps = conn.prepareStatement(sql);
rs = ps.executeQuery(sql);
} catch (SQLException e) {
System.out.println("sql数据库查询异常");
e.printStackTrace();
}
return rs;
}
public static void closeConn() {
try {
conn.close();
}catch(SQLException e) {
System.out.println("sql数据库关闭异常");
e.printStackTrace();
}
}
}
本人新手 麻烦给出尽量详细的指导 展开
import java.sql.*;
public class DBconn {
static String url = "jdbc:mysql://localhost:3306/test?useunicode=true& characterEncoding=utf8";
static String username = "root";
static String password = "1234";
static Connection conn = null;
static ResultSet rs = null;
static PreparedStatement ps = null;
public static void init() {
if( conn == null){
try {
Class.forName("mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, username, password);
} catch (Exception e) {
System.out.println("init [SQL驱动程序初始化失败!]");
e.printStackTrace();
}
try {
Class.forName("mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, username, password);
} catch (Exception e) {
System.out.println("init [SQL驱动程序初始化失败!]");
e.printStackTrace();
}
}
}
public static int addUpdDel(String sql) {
int i = 0;
try {
PreparedStatement ps = conn.prepareStatement(sql);
i = ps.executeUpdate();
} catch (SQLException e) {
System.out.println("sql数据库增删改异常");
e.printStackTrace();
}
return i;
}
public static ResultSet selectSql(String sql) {
try {
ps = conn.prepareStatement(sql);
rs = ps.executeQuery(sql);
} catch (SQLException e) {
System.out.println("sql数据库查询异常");
e.printStackTrace();
}
return rs;
}
public static void closeConn() {
try {
conn.close();
}catch(SQLException e) {
System.out.println("sql数据库关闭异常");
e.printStackTrace();
}
}
}
本人新手 麻烦给出尽量详细的指导 展开
2个回答
展开全部
JDBC技术实战你用conn创建PreparedStatement,却位初始化conn,也就是你每次要查询的时候,请先调用init方法,如
public static int addUpdDel(String sql) {
int i = 0;
try {
init(); //先创建链接 conn
PreparedStatement ps = conn.prepareStatement(sql);
i = ps.executeUpdate();
} catch (SQLException e) {
System.out.println("sql数据库增删改异常");
e.printStackTrace();
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询