81,092
社区成员
发帖
与我相关
我的任务
分享
// 5.建立连接
public Connection getConn() {
try {
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test", "root", "");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
// 6.执行操作
public void insertUser(String userName, String userPwd) {
try {
stmt = this.getConn().createStatement();
stmt.execute("insert into users values" + "('" + userName + "','" + userPwd + "')");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
package org.jg.lohas.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
//1.加载厂商驱动
//2.加载jdbc包
//3.载入驱动对象
public class DBConn {
// 4.将驱动对象加载到内存中去
private Connection conn;
private Statement stmt;
static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 5.建立连接
public void getConn() {
try {
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/test", "root", "");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 6.执行操作
public void insertUser(String userName, String userPwd) {
try {
stmt = conn.createStatement();
stmt.execute("insert into users values" + "('" + userName + "','" + userPwd + "')");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public boolean IsUser(String userName, String userPwd) {
boolean flag = false;
try {
stmt = conn.createStatement();
ResultSet rs = stmt
.executeQuery("select * from users where userName='"
+ userName + "' and userPwd='" + userPwd + "'");
if (rs != null && rs.next()) {
flag = true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return flag;
}
public void insertLog(String titleName,String contents){
try {
stmt = conn.createStatement();
stmt.execute("insert into blog(titleName,contents)values" + "('"
+ titleName + "','" + contents + "')");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 关闭
public void closeConn() {
try {
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void insertUser(String userName, String userPwd) {
try {
stmt = conn.createStatement();
stmt.execute("insert into users(userName,userPwd)values" + "('"
+ userName + "','" + userPwd + "')");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public class ReUser extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String userName = request.getParameter("userName");
String userPwd = request.getParameter("userPwd");
DBConn conn = new DBConn();
conn.getConn();
conn.insertUser(userName,userPwd);
conn.closeConn();
}
}
<body>
<form action = "servlet/ReUser" method="post">
<table width="672" height="267" align="center" >
<tr><td align="center"><br><br><br>用户注册<br><br><br><br><br></td></tr>
<tr><td>昵称:<input type="text" name="userName"/> * 昵称由2到24个字符组成</td></tr>
<tr><td> 密 码: <input type="password" name="userPwd"/> * 密码必须是6到16个字符,建议使用英文和符号混合</td></tr>
<tr><td align="center"> <input type="submit" name="userName" value="注册新用户"/> <input type="reset" name="userName" value="重写"/></td></tr>
</table>
</form>
</body>