我正在尝试将ec数据从日食氧气插入SqlServer DB中。我也已经成功安装了jdbc驱动程序sqljdbc41和Tomcat服务器9。
当我单击“添加数据”按钮时出现错误,以下是DatabaseDemo.html和DataBase.java的代码
DatabaseDemo.html:
<form method="post" action="./DataBase">
name : <input type="text" name ="txtname"/><br><br>
address : <textarea name="txtadr" rows="5" cols="10"></textarea><br><br>
contact : <input type="text" name="txtcnt"/><br><br>
age : <input type="text" name="txtage"/><br><br>
<input type="submit" value="add data"/>
<input type="reset" value="clear"/>
</form>
DataBase.java:
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Statement;
import com.microsoft.sqlserver.jdbc.SQLServerDriver;
import java.sql.DriverManager;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import java.sql.Connection;
import java.sql.ResultSet;
/**
* Servlet implementation class DataBase
*/
@WebServlet("/DataBase")
public class DataBase extends HttpServlet {
private static final long serialVersionUID = 1L;
Connection con;
Statement stmt;
String name, address;
int contact, age;
String dbString="jdbc:sqlserver://localhost; databaseName=Emp; tableName=emp1; integratedSecurity=true; user=sa; password=qwerty";
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
try {
name=request.getParameter("txtname");
address=request.getParameter("txtadr");
contact=Integer.parseInt(request.getParameter("txtcnt"));
age=Integer.parseInt(request.getParameter("txtage"));
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con=DriverManager.getConnection(dbString, "sa", "siva123");
stmt = con.createStatement();
String query="insert into emp1 values('"+name+"','"+address+"',"+contact+","+age+");";
stmt.execute(query);
System.out.println("Data Inserted Succesfully");
} catch (Exception e) {
// TODO: handle exception
System.out.println("Data not Inserted");
e.printStackTrace();
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
注意:我是编程新手,所以有人可以帮助我吗