mysql创建statement对象
关注:56 答案:4 mip版
解决时间 2021-01-24 21:05
提问者西伯利亚狼
2021-01-24 13:31
package com.yxq.toolsbean;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DB {
private final String url = "jdbc:mysql://localhost:3306/mysql";
private final String userName = "root";
private final String password = "";
private Connection conn = null;
private Statement stm=null;
public DB(){
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("加载数据库成功!");
} catch (Exception e) {
e.printStackTrace();
System.out.println("加载数据库驱动失败!");
}
}
public void createCon() {
try {
Connection conn = DriverManager.getConnection(url, userName, password);
System.out.println("获取数据库连接成功!");
} catch (Exception e) {
e.printStackTrace();
System.out.println("获取数据库连接失败!");
}
}
public void getStm(){
createCon();
try {
Statement stm=conn.createStatement();
System.out.println("创建Statement对象成功!");
} catch (SQLException e) {
e.printStackTrace();
System.out.println("创建Statement对象失败!");
}
}
我用测试类test测试以上代码
package com.yxq.toolsbean;
public class Test {
public static void main(
String[] args) {
DB db=new DB();
db.createCon();
db.getStm();
}
}
报错为
加载数据库成功!
获取数据库连接成功!
获取数据库连接成功!
Exception in thread "main" java.lang.NullPointerException
at com.yxq.toolsbean.DB.getStm(DB.java:40)
at com.yxq.toolsbean.Test.main(Test.java:10)
有空你们自己把代码粘贴到myeclipse看一下
最佳答案
二级知识专家归人
2021-01-24 14:37
在JDBC里面,statement只是一个接口并没有实现?那为什么能够创建statement对象?
不要思维定式在 Statement stmt = new Statement();才是创建对象 这种思维上.
创建对象方式有很多 我给你写了个类似的 名字也用了Statement不过只是个名字而已 真实的Statement实现方法你可以看源码,我的代码只是告诉你"创建对象方式有很多"
public interface Statement {
}
public class Preparedment implements Statement {
public static Statement getSatementInstance(){
return new Preparedment();
}
}
现在你可以用以下来创建 一个接口 的对象
Statement stm1 = Preparedment.getSatementInstance();
Statement stm2 = new Preparedment();
全部回答
1楼这辈子只爱你
2021-01-24 17:34
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DBConn {
private Connection conn;
String strMysqlURL = "jdbc:mysql://127.0.0.1:3306/bookroom?user=root&password=arone";
public Connection getConnection() {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection(strMysqlURL);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
public static void main(String args[]) {
DBConn con = new DBConn();
con.getConnection();
}
public DBConn() { // 初始化时建立数据源的连接
this.getConnection();
}
public synchronized Statement getStmt() throws Exception {
Statement stmt;
stmt = conn.createStatement();
return stmt;
}
// 创建预处理会话对象
public synchronized PreparedStatement getPstmt(String sql) throws Exception {
PreparedStatement pstmt=null;
pstmt = conn.prepareStatement(sql);
return pstmt;
}
// 执行查询操作
public synchronized ResultSet query(String sql) throws Exception {
try {
Statement stmt;
stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
return rs;
} catch (Exception e) {
System.out.println(e.toString());
return null;
}
}
public boolean hasUserName(String sql) { // 判断重复
try {
Statement stmt;
stmt = getConnection().createStatement();
ResultSet rs = stmt.executeQuery(sql);
rs.next();
if (rs.getString("num").equals("0")) {
return false;
} else {
return true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
// 执行更新操作
public synchronized int update(String sql) throws Exception {
int nn=0;
Statement stmt=null;
try {
stmt = conn.createStatement();
nn = stmt.executeUpdate(sql);
} catch (Exception e) {
System.out.println(e.toString());
nn = 0;
}
return nn;
}
// 关闭数据库连接
public void DBclose() throws SQLException {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
2楼儚迴从偂
2021-01-24 17:08
java.sql里面封装的是类可以支持所有jdbc规范的数据库软件,如oracle和sqlserver等。
使用java.sql不存在兼容性问题。
com.mysql.jdbc 是专门针对mysql 优化过的
3楼月洸芐啲溫拵
2021-01-24 16:04
。。靠了,肯定错啊,你那个createCon中的Connection conn是局部变量而且他的值改变又不影响类中的那个Connection对象。你把createCon中别定义Connection conn,直接conn,肯定对
直接用createCon() 返回Connection对象多好,一般都这么写
还有连接两次数据库,没必要,也很危险。
我要举报
如果感觉以上信息为低俗/不良/侵权的信息,可以点下面链接进行举报,我们会做出相应处理,感谢你的支持!
点此我要举报以上信息!
推荐资讯
大家都在看