java连接数据库,在文本框中输入数据保存到数据库的表中,能举个例子
1个回答
展开全部
<html>
<form method="post" action="Insert.jsp"> //form start
<table>
<tr><td>Fname:</td><td><input type="text" name="fname"></td></tr>
<tr><td>LName:</td><td><input type="text" name="lname"></td></tr>
<tr><td>Email:</td><td><input type="text" name="email"></td></tr>
<tr><td>pass:</td><td><input type="password" name="pass"></td></tr>
<tr><td>confirm pass:</td><td><input type="password" name="cpass"></td></tr>
<tr><td>dob:</td><td><input type="text" name="dob"></td></tr>
<tr><td>age:</td><td><input type="text" name="age"></td></tr>
<tr><td>gender:</td><td><input type="text" name="gender"></td></tr>
<tr><td>address:</td><td><input type="text" name="address"></td></tr>
<tr><td>country:</td><td><input type="text" name="country"></td></tr>
<tr><td>state:</td><td><input type="text" name="state"></td></tr>
<tr><td>city:</td><td><input type="text" name="city"></td></tr>
<tr><td>telephone:</td><td><input type="text" name="tno"></td></tr>
<tr><td>Mobile:</td><td><input type="text" name="mobile"></td></tr>
<tr><td></td><td><input type="submit" value="Submit"></td></tr>
</table>
</form>
</html>
/*
Insert.jsp
*/
<%@page import="java.sql.*,java.util.*"%> // import
<%
String fname=request.getParameter("fname");
String lname=request.getParameter("lname");
String email=request.getParameter("email");
String pass=request.getParameter("pass");
String cpass=request.getParameter("cpass");
String dob=request.getParameter("dob");
int age=Integer.parseInt(request.getParameter("age"));
String gender=request.getParameter("gender");
String address=request.getParameter("address");
String country=request.getParameter("country");
String state=request.getParameter("state");
String city=request.getParameter("city");
int tno=Integer.parseInt(request.getParameter("tno"));
int mobile=Integer.parseInt(request.getParameter("mobile"));
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/student", "root", "");
Statement st=con.createStatement();
int i=st.executeUpdate("insert into student(fname,lname,email,pass,cpass,dob,age,gender,address,country,state,city,tno,mobile) values('"+fname+"','"+lname+"','"+email+"','"+pass+"','"+cpass+"','"+dob+"','"+age+"','"+gender+"',"+address+",'"+country+"','"+state+"','"+city+"','"+city+"','"+tno+"','"+mobile+"')");
out.println("Data is successfully inserted!");
}
catch(Exception e){
System.out.print(e);
e.printStackTrace();
}
%>
<form method="post" action="Insert.jsp"> //form start
<table>
<tr><td>Fname:</td><td><input type="text" name="fname"></td></tr>
<tr><td>LName:</td><td><input type="text" name="lname"></td></tr>
<tr><td>Email:</td><td><input type="text" name="email"></td></tr>
<tr><td>pass:</td><td><input type="password" name="pass"></td></tr>
<tr><td>confirm pass:</td><td><input type="password" name="cpass"></td></tr>
<tr><td>dob:</td><td><input type="text" name="dob"></td></tr>
<tr><td>age:</td><td><input type="text" name="age"></td></tr>
<tr><td>gender:</td><td><input type="text" name="gender"></td></tr>
<tr><td>address:</td><td><input type="text" name="address"></td></tr>
<tr><td>country:</td><td><input type="text" name="country"></td></tr>
<tr><td>state:</td><td><input type="text" name="state"></td></tr>
<tr><td>city:</td><td><input type="text" name="city"></td></tr>
<tr><td>telephone:</td><td><input type="text" name="tno"></td></tr>
<tr><td>Mobile:</td><td><input type="text" name="mobile"></td></tr>
<tr><td></td><td><input type="submit" value="Submit"></td></tr>
</table>
</form>
</html>
/*
Insert.jsp
*/
<%@page import="java.sql.*,java.util.*"%> // import
<%
String fname=request.getParameter("fname");
String lname=request.getParameter("lname");
String email=request.getParameter("email");
String pass=request.getParameter("pass");
String cpass=request.getParameter("cpass");
String dob=request.getParameter("dob");
int age=Integer.parseInt(request.getParameter("age"));
String gender=request.getParameter("gender");
String address=request.getParameter("address");
String country=request.getParameter("country");
String state=request.getParameter("state");
String city=request.getParameter("city");
int tno=Integer.parseInt(request.getParameter("tno"));
int mobile=Integer.parseInt(request.getParameter("mobile"));
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/student", "root", "");
Statement st=con.createStatement();
int i=st.executeUpdate("insert into student(fname,lname,email,pass,cpass,dob,age,gender,address,country,state,city,tno,mobile) values('"+fname+"','"+lname+"','"+email+"','"+pass+"','"+cpass+"','"+dob+"','"+age+"','"+gender+"',"+address+",'"+country+"','"+state+"','"+city+"','"+city+"','"+tno+"','"+mobile+"')");
out.println("Data is successfully inserted!");
}
catch(Exception e){
System.out.print(e);
e.printStackTrace();
}
%>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询