[b]关于注册/登录网页连接到数据库:[/b]
注册:
跳转代码:
[b]登录:[/b]
[b]显示列表sql:[/b]
[b]跳转到修改页面并且把所要修改行的“id”传过去:[/b]
其中id=<%=rs.getString(1)%>为传ID
[b]跳转到删除页面并且把所要删除行的“id”传过去:[/b]
注册:
String id = request.getParameter("id");
String member = request.getParameter("member");
String pass = request.getParameter("pass");
String name = request.getParameter("name");
try {
//1,加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2,建立连接
java.sql.Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/sht", "root", "root");
//3,创建sql传送对象
Statement stmt = conn.createStatement();
//4,将sql语句通过sql传送对象传送到数据库并执行,返还结果集
String sql = "insert into member value('"+id+"','"+member+"','"+pass+"','"+name+"')";
stmt.executeUpdate(sql);
stmt.close();
conn.close();
跳转代码:
response.sendRedirect("login.html");//跳转到login.html
[b]登录:[/b]
<form id="form1" name="form1" method="post" action="dologin.jsp">//表单登录提交之后跳转到dologin.jsp
[b]显示列表sql:[/b]
try{
Class.forName("com.mysql.jdbc.Driver");
java.sql.Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/sht","root","root");
Statement stmt = conn.createStatement();
String sql = "select * from member";
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()){
[b]跳转到修改页面并且把所要修改行的“id”传过去:[/b]
<a href="update.jsp?id=<%=rs.getString(1)%>">修改</a>
其中id=<%=rs.getString(1)%>为传ID
try {
//1,加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2,建立连接
java.sql.Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/sht", "root", "root");
//3,创建sql传送对象
Statement stmt = conn.createStatement();
//4,将sql语句通过sql传送对象传送到数据库并执行,返还结果集
String sql = "update member set member ='"+member+"',pass='"+pass+"',name='"+name+"' where id='"+id+"'";//更新数据
stmt.executeUpdate(sql);
stmt.close();
conn.close();
} catch (Exception e) {
System.out.println(e);
}
response.sendRedirect("dolist.jsp");
[b]跳转到删除页面并且把所要删除行的“id”传过去:[/b]
<a href="delete.jsp?id=<%=rs.getString(1)%>">
try {
//1,加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2,建立连接
java.sql.Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/sht", "root", "root");
//3,创建sql传送对象
Statement stmt = conn.createStatement();
//4,将sql语句通过sql传送对象传送到数据库并执行,返还结果集
String sql = "delete from member where id='"+id1+"'";//删除数据delete from member
stmt.executeUpdate(sql);
stmt.close();
conn.close();
} catch (Exception e) {
System.out.println(e);
}
response.sendRedirect("dolist.jsp");