本人最近需要用JSP做一个网站,但是本人之前没学过JSP,我想问一下我在一个页面布置了一个类似于表格的东西,需要别人填写信息,然后将这些信息传送到MySQL数据库中,我只编出了界面,但是不知道接下来怎么做,请各位高手指点一下
<tr>
<th width=160>毕业设计(论文)题目</th>
<th width=300><input type=text value=""></th>
</tr>
<tr>
<th width=160>院 (部)</th>
<th width=300><input type=text value=""></th>
<th width=160>指导老师</th>
<th width=300><input type=text value=""></th>
</tr>
<tr>
<th width=160>专业</th>
<th width=300><input type=text value=""></th>
<th width=160>班级</th>
<th width=300><input type=text value=""></th>
</tr>
<tr>
<th width=160>学生学号</th>
<th width=300><input type=text value=""></th>
<th width=160>学生姓名</th>
<th width=300><input type=text value=""></th>
</tr>
<tr>
<th width=160>课题来源</th>
<th width=300><input type=text value=""></th>
<th width=160>课题类别</th>
<th width=300><input type=text value=""></th>
</tr>
<tr>
<th width=160>课题来源</th>
<th width=300><input type=text value=""></th>
<th width=160>课题类别</th>
<th width=300><input type=text value=""></th>
</tr>
个人认为要在input type=text value=""里面填写一些东西,但不知道怎么填写,另外<form>里面需要改么,求指点
<tr>
<th width=160>毕业设计(论文)题目</th>
<th width=300><input type=text value=""></th>
</tr>
<tr>
<th width=160>院 (部)</th>
<th width=300><input type=text value=""></th>
<th width=160>指导老师</th>
<th width=300><input type=text value=""></th>
</tr>
<tr>
<th width=160>专业</th>
<th width=300><input type=text value=""></th>
<th width=160>班级</th>
<th width=300><input type=text value=""></th>
</tr>
<tr>
<th width=160>学生学号</th>
<th width=300><input type=text value=""></th>
<th width=160>学生姓名</th>
<th width=300><input type=text value=""></th>
</tr>
<tr>
<th width=160>课题来源</th>
<th width=300><input type=text value=""></th>
<th width=160>课题类别</th>
<th width=300><input type=text value=""></th>
</tr>
<tr>
<th width=160>课题来源</th>
<th width=300><input type=text value=""></th>
<th width=160>课题类别</th>
<th width=300><input type=text value=""></th>
</tr>
个人认为要在input type=text value=""里面填写一些东西,但不知道怎么填写,另外<form>里面需要改么,求指点
1.获取数据库连接
public static Connection getConnection() throws SQLException{
String driver="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/test";
String user="root";
String pwd="admin";
Connection conn=null;
try
{
Class.forName(driver);
conn = DriverManager.getConnection(url, user, pwd);
}
catch (Exception e)
{
e.printStackTrace();
}finally{
if(conn != null){
conn.close();
}
}
return conn;
}2.插入记录到数据库
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
String usernameString = request.getParameter("username");
Connection con = getConnection();
String sql = "insert into adduser (op_name,op_time) values (?,sysdate())";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, "dunsan");
boolean b = pstmt.execute();
}
input type="text" value=""
后面应该还有个什么东西,好像是导入数据库所用到的方法,那个应该怎么写
找的一个JAVABEAN,
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;public class DB {
public static Connection getConn() {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bbs", "root" , "123456");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static Statement createStmt(Connection conn) {
Statement stmt = null;
try {
stmt = conn.createStatement();
} catch (SQLException e) {
e.printStackTrace();
}
return stmt;
}
public static ResultSet executeQuery(Statement stmt, String sql) {
ResultSet rs = null;
try {
rs = stmt.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
/*public static ResultSet executeQuery(Connection conn, String sql) {
Statement stmt = null;
ResultSet rs = null;
try {
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
} catch (SQLException e) {
e.printStackTrace();
}
// finally {
// close(stmt);
// }
return rs;
}*/
public static int executeUpdate(Connection conn, String sql) {
int ret = 0;
Statement stmt = null;
try {
stmt = conn.createStatement();
ret = stmt.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
} finally {
close(stmt);
}
return ret;
}
public static PreparedStatement prepareStmt(Connection conn, String sql) {
PreparedStatement pstmt = null;
try {
pstmt = conn.prepareStatement(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return pstmt;
}
public static PreparedStatement prepareStmt(Connection conn, String sql, int autoGeneratedKeys) {
PreparedStatement pstmt = null;
try {
pstmt = conn.prepareStatement(sql, autoGeneratedKeys);
} catch (SQLException e) {
e.printStackTrace();
}
return pstmt;
}
public static void close(Connection conn) {
if(conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
conn = null;
}
}
public static void close(Statement stmt) {
if(stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
stmt = null;
}
}
public static void close(ResultSet rs) {
if(rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
rs = null;
}
}
}add.jsp
<form>
<table>
<tr>
<td>
<input type="text" name="x1" id="x1">
......
</td>
</tr>
</table>
</form>
Into_data.jsp
<%@ page language="java" import="java.util.*, java.sql.*, yuhaibin.*" pageEncoding="GB18030"%>
<% String x1=request.getParameter("x1");
.......
Connection conn=DB.getConn;
conn.setAutoCommit(false);
String sql="insert into datatable values(x1,...)";
Statement stmt=DB.createStmt(conn);
stmt.executeUpdate(sql);
conn.commit();
conn.setAutoCommit(true);
DB.close(stmt);
DB.close(conn);
...
%>
<td><div align="right">用户名:</div></td>
<td><input name="cname" type="text" id="cname"></td>
</tr>
LZ可以看看LS 的这个正解……多看看JSP的内置对象和标签库,基本上就可以掌握JSP了……
<form action="" method="post">
<table></table>
<input type="submit" value="提交"/>
</form>
action=""中写的是你需要提交给的连接地址
例如:action="项目名字/包名/xxx";
上面那个xxx连接的名字是你在web.xml里进行配置的名字。
譬如你先要把网页那边穿过的的数据拿过来是吧:
String username=request.getParameter("name");
"name":表示的是你jsp页面中传过来的标签的名字
例如:
<td><input name="name" type="text" id="name"/></td>
这样你输出看看能拿到你页面中传过来的值了。
System.out.println(name);
如果楼主会用hibernate的话。可以把这些传过的值封转到你也对象里然后再插入到数据库中就行了。
如果楼主会用hibernate的话。可以把这些传过来的值封装到对象里,然后插入到数据库里就行了。
form 的action属性里面填写你要提交到那个页面,提交到本页也可以,然后在那个页面里接收参数,然后通过JDBC方式写入数据库就可以了