你的这些代码需要放在try...catch中,因为这些代码都有可能抛出异常,而你没有处理这些异常,比如:
try
{
Class.forName(CLASSFORNAME);
}catch(ClassNotFoundException e){
... //不处理也没关系,只要catch了就ok
}
try
{
Class.forName(CLASSFORNAME);
}catch(ClassNotFoundException e){
... //不处理也没关系,只要catch了就ok
}
public InsertServlet() {
String CLASSFORNAME = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String DBURL = "jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=jspdev";
String USER = "sa";
String PWD = "sa"; try {
Class.forName(CLASSFORNAME);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
conn = DriverManager.getConnection(DBURL, USER, PWD);
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} } public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { Statement stmt = null;
try {
stmt = conn.createStatement();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String SQLSTR = "insert into myuser(identify_id,reg_id,user_name,user_state,user_pwd,flag values('D1001','5566','com','00','',0)"; try {
stmt.executeUpdate(SQLSTR);
stmt.close();
conn.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} } //end of doGet()
Class.forName(CLASSFORNAME).newInstance();
希望我没打错
这不是说就没有Statement 对象吗?那就是conn没有createStatement()啊?快哭了啊
InsertServlet.java:55: cannot resolve symbol
symbol : variable stmt
location: class wht.InsertServlet
stmt.executeUpdate(SQLSTR);
^
InsertServlet.java:56: cannot resolve symbol
symbol : variable stmt
location: class wht.InsertServlet
stmt.close();
^
2 errors
---------------------------------------
package wht;import javax.servlet.*;
import javax.servlet.http.*;
import com.microsoft.jdbc.sqlserver.SQLServerDriver;
import java.sql.*;
import java.util.*;
import java.io.*;public class InsertServlet extends HttpServlet
{ private Connection conn; public InsertServlet()
{
String CLASSFORNAME="com.microsoft.jdbc.sqlserver.SQLServerDriver";
String DBURL="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=jspdev";
String USER="sa";
String PWD="sa";
try{
Class.forName(CLASSFORNAME);
}
catch(SQLException e){
e.printStackTrace();
} try{
conn = DriverManager.getConnection(DBURL,USER,PWD);
}
catch(SQLException se){
se.printStackTrace();
}
}
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{ try{
Statement stmt = conn.createStatement();
}
catch(SQLException e){
e.printStackTrace();
} String SQLSTR = "insert into myuser(identify_id,reg_id,user_name,user_state,user_pwd,flag values('D1001','5566','com','00','',0)";
try{
stmt.executeUpdate(SQLSTR);
stmt.close();
conn.close(); }
catch(SQLException el){
el.printStackTrace();
} }//end of doGet() public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException,ServletException
{
doGet(request,response);
} //end of doPost()
}
Statement stmt = conn.createStatement();
}你声明stmt的作用域只在此try区域内有效。
把stmt声明为全局对象
为什么那?
InsertServlet.java:26: unreported exception java.lang.ClassNotFoundException; mu
st be caught or declared to be thrown
Class.forName(CLASSFORNAME);
^
1 error
-----------------------------------------------
public class InsertServlet extends HttpServlet
{ private Connection conn;
private Statement stmt ; public InsertServlet()
{
String CLASSFORNAME="com.microsoft.jdbc.sqlserver.SQLServerDriver";
String DBURL="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=jspdev";
String USER="sa";
String PWD="sa";
try{
Class.forName(CLASSFORNAME);
}
catch(SQLException e){
e.printStackTrace();
} try{
conn = DriverManager.getConnection(DBURL,USER,PWD);
}
catch(SQLException se){
se.printStackTrace();
}
}
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{ try{
stmt = conn.createStatement();
}
catch(SQLException e){
e.printStackTrace();
} String SQLSTR = "insert into myuser(identify_id,reg_id,user_name,user_state,user_pwd,flag values('D1001','5566','com','00','',0)";
try{
stmt.executeUpdate(SQLSTR);
stmt.close();
conn.close(); }
catch(SQLException el){
el.printStackTrace();
} }//end of doGet() public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException,ServletException
{
doGet(request,response);
} //end of doPost()