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 Update {
public static void main(String[] args) throws SQLException {
// TODO Auto-generated method stub
query("iuoi");//空指针异常
}
public static void delete() throws SQLException
{
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Connection conn = null;
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc1","root","");
ResultSet rs=null;
String query = "select * from exam_student";
String delete ="delete from exam_student where height=165";
PreparedStatement pr = conn.prepareStatement(delete);
int i = pr.executeUpdate();
rs=pr.executeQuery(query);
while(rs.next())
{
//System.out.println(pr.executeUpdate());
System.out.println(rs.getObject(1)+" "+i);
}
}
public static void query(String name) throws SQLException
{
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Connection conn = null;
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc1","root","");
Statement st = null;
ResultSet rs=null;
String query = "select name,str_no,height from exam_student where name='"+name+"'";
//String delete ="delete from exam_student where height=165";
//PreparedStatement pr = conn.prepareStatement(delete);
//int i = pr.executeUpdate();
rs=st.executeQuery(query);//此处抱空指针异常
while(rs.next())
{
//System.out.println(pr.executeUpdate());
System.out.println(rs.getObject(1)+" "+rs.getObject(2)+" "+rs.getObject(3));
}
}
}
------解决思路----------------------
64行Statement st =conn.createStatement();
------解决思路----------------------
楼上正解,st就没得到实例化对象,光一声明,当然空指针
------解决思路----------------------
楼上正解 statement st没有实例化,,怎么会不出错吗
------解决思路----------------------
st在声明时为null,后面的代码中没有使用Connection为该变量赋值,当然会报空指针异常了
------解决思路----------------------
Statement st =conn.createStatement();加上就行了
------解决思路----------------------
楼上说的对,不过你可以试试apache的一个连接工具类dbutils,很方便,不用自己动手写
------解决思路----------------------
楼主太懒了,空指针异常其实是最好查的错误,用eclipse或netbeans之类的IDE工具调试一下,很快就找到原因了
------解决思路----------------------
简化一下,大约就是这样的代码
当然会报空指针异常,代码贴到Eclipse里,没运行之前就提示这个问题了
Statement st = null;
rs = st.executeQuery(query);// 此处抱空指针异常
------解决思路----------------------
楼上都是正解。。。
------解决思路----------------------
LS 都比较厉害啊
------解决思路----------------------
还是要多写代码,呵呵~
------解决思路----------------------
你的Statement都没有初始化呀,肯定是报空指针异常
Statement st = conn.creatStatement();
------解决思路----------------------
F6一步步调试,看变量值。
------解决思路----------------------
楼主代码结构有点乱啊,一般新手不是应该把加载驱动、创建链接以及销毁放在一个工厂类里面完成吗?jdbcutils,加油小伙儿
------解决思路----------------------
st , rs 都是null,未指定实例