java有关PreparedStatement问题??
java可视化中我想向数据库中添加数据用了如下语句,总是到PreparedStatementpstmt=Application.con.prepareStatement(...
java可视化中我想向数据库中添加数据用了如下语句,总是到PreparedStatement pstmt = Application.con.prepareStatement(sql);停止执行 请帮忙改下。我在非可视化用这个语句是对的啊??
complish.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
String Sno = text1.getText();
String Sname = text2.getText();
try {
String sql = "insert into Student values(?,?)";
PreparedStatement pstmt = Application.con.prepareStatement(sql);//在此处被抛出异常
pstmt.setString(1, Sno);
pstmt.setString(2, Sname);
pstmt.execute();
pstmt.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
java.lang.NullPointerException
at Insert$3.widgetSelected(Insert.java:189)
at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:248)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1053)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4169)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3758)
at Insert.open(Insert.java:55)
at Insert.main(Insert.java:40) 展开
complish.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
String Sno = text1.getText();
String Sname = text2.getText();
try {
String sql = "insert into Student values(?,?)";
PreparedStatement pstmt = Application.con.prepareStatement(sql);//在此处被抛出异常
pstmt.setString(1, Sno);
pstmt.setString(2, Sname);
pstmt.execute();
pstmt.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
java.lang.NullPointerException
at Insert$3.widgetSelected(Insert.java:189)
at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:248)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1053)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4169)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3758)
at Insert.open(Insert.java:55)
at Insert.main(Insert.java:40) 展开
展开全部
把异常信息贴出来看一下,还有标准的写法不是这样的,你上面的写法容易造成连接泄露,应该是:
Connection conn = getConn();
try{
Statement stmt = conn.createStatement();
try{
ResultSet rs = stmt.executeQuery("sql");
try{
//do sth
}finally{
rs.close();
}
}finally{
stmt.close();
}
}finally{
conn.close();
}
Connection conn = getConn();
try{
Statement stmt = conn.createStatement();
try{
ResultSet rs = stmt.executeQuery("sql");
try{
//do sth
}finally{
rs.close();
}
}finally{
stmt.close();
}
}finally{
conn.close();
}
更多追问追答
追问
异常信息:
java.lang.NullPointerException
at Insert$3.widgetSelected(Insert.java:189)
at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:248)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
追答
Application.con检查一下这个参数,执行这个事件的时候有没有实例化,还有获取一个连接不要这么写,数据库连接是一种资源,不要做成全局的变量,别的地方关闭了,就没法用了
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询