您好我正在尝试使用 jdbc 将值插入 SQL 数据库。当前代码能够连接和查询 sql DB。我正在使用 executeupdate 插入值。这段代码没有给出任何错误。但是值没有被插入到 SQL DB 中,即使我厌倦了在执行之后和之前提交代码。我怎样才能将值插入数据库???
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import oracle.jdbc.driver.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Servlet1 extends HttpServlet {
protected void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{
String p1=request.getParameter("param1");
String p2=request.getParameter("param2");
String p3=request.getParameter("param3");
String p4=request.getParameter("param4");
String p5=request.getParameter("param5");
String sql="INSERT INTO EMPLOYEES (Id,Name,Post,Salary,Location) VALUES('" + p1 + "','" + p2 + " ','" + p3 + "','" + p4 + " ','" + p5 + " ')";
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
}
catch(ClassNotFoundException ex)
{
ex.printStackTrace();
}
Connection con=null;
Statement stmt=null;
try
{
con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521/XE","test","Gari!2563");
stmt=con.createStatement();
stmt.executeUpdate(sql);
con.setAutoCommit(true);
}
catch(SQLException ex)
{
ex.printStackTrace();
}
finally
{
try
{
if(stmt!=null)
{
stmt.close();
stmt=null;
}
}
catch(SQLException ex)
{
ex.printStackTrace();
}
try
{
if(con!=null)
{
con.close();
con=null;
}
}
catch(SQLException ex)
{
ex.printStackTrace();
}
}
PrintWriter out = response.getWriter();
out.println("done");
}
}
我正在使用 jdk1.7,Oracle DB XE 11.2.0。Apache tomcat 7 eclipse juno EE 版。