黑马程序员技术交流社区
标题:
jdbc出现问题
[打印本页]
作者:
冯超
时间:
2013-3-15 22:09
标题:
jdbc出现问题
本帖最后由 冯超 于 2013-3-16 15:48 编辑
package jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
//工具类
public final class JdbcUtils {
private static String url = "jdbc:mysql://localhost:3306/jdbc";
private static String user = "root";
private static String password = "5512013025";
private JdbcUtils() {
}
static {
//注册驱动
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url,user,password);
}
public static void free(ResultSet rs, Statement st, Connection conn) {
try {
if(rs != null)
rs.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if(st != null)
st.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if(conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
}
复制代码
package jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Base {
/**
* @param args
* @throws ClassNotFoundException
* @throws SQLException
*/
public static void main(String[] args) throws SQLException, ClassNotFoundException {
// TODO Auto-generated method stub
//test();
template();
}
//正规的jdbc码 模板如下
@SuppressWarnings("null")
static void template() throws SQLException
{
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try {
// 2.建立连接
JdbcUtils.getConnection();
//3.创建语句
st = conn.createStatement();
//4.执行语句
rs = st.executeQuery("select * from user");
//5.处理结果
while(rs.next()) {
System.out.println(rs.getObject(1) + "\t" +
rs.getObject(2) +
rs.getObject(3) + "\t" +
rs.getObject(4) + "\t");
}
} finally {
JdbcUtils.free(rs, st, conn);
}
}
复制代码
为什么出现空指针异常
当我把他们写在一个函数的时候
public static void test() throws SQLException, ClassNotFoundException {
//1.注册驱动
//DriverManager.registerDriver(new com.mysql.jdbc.Driver());//方法一
//System.setProperty("jdbc.drivers","com.mysql.jdbc.Driver");//方法二
Class.forName("com.mysql.jdbc.Driver"); //方法三 最好
//2.建立连接
String url = "jdbc:mysql://localhost:3306/jdbc";
String user = "root";
String password = "5512013025";
Connection conn = DriverManager.getConnection(url,user,password);
//3.创建语句
Statement st = conn.createStatement();
//4.执行语句
ResultSet rs = st.executeQuery("select * from user");
//5.处理结果
while(rs.next()) {
System.out.println(rs.getObject(1) + "\t" +
rs.getObject(2) +
rs.getObject(3) + "\t" +
rs.getObject(4) + "\t");
}
System.out.println("aaaaaaaaabbbbbbbbbbb");
//释放资源
rs.close();
st.close();
conn.close();
}
复制代码
就没问题,不知道为什么?
作者:
冯超
时间:
2013-3-15 22:10
为什么英文都转化成我亲切的汉语了
作者:
陈腾跃_2013
时间:
2013-3-15 23:00
本帖最后由 陈腾跃_2013 于 2013-3-15 23:03 编辑
我是还没学到JDBC,不过看你的代码:
“当我把他们写在一个函数的时候” 的代码有:“Connection conn = DriverManager.getConnection(url,user,password);”
但是上面两块代码第二块第30行:“JdbcUtils.getConnection();”
其中,getConnection(); 是你自定义的一个方法,方法体中有DriverManager.getConnection(url,user,password);
根据这个判断30行应该是conn = JdbcUtils.getConnection(); 吧,你没有给conn赋值。
不知道说的对不对,有学到JDBC的朋友们欢迎拍砖斧正
作者:
冯超
时间:
2013-3-16 12:19
en`发帖了 我发现了这个问题
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2