Connection
con=
DriverManager.getConnection("jdbc:mysql://10.10.2.188:3306/bbs?useUnicode=true&characterEncoding=utf8",
"root","yaoweijq")
Statement
stat=
con.createStatement()
第一句是加载数据库驱动,
自己下一个加到类库中,
我不知道要不要放到tomcat目录下。
第二句是连接数据库,
IP自己改,测试用localhost
3306是端口,mysql的
后面是数据库名你也可以用表名
再后面root是用户名yaoweijq是密码
都是mysql的
大体就是这样
本例设计到三个类和两个页面User类(数据实体),UserManager(封装了Dao),DBUtil(封装了数据库常用 *** 作)index页面(添加用户页面),add页面(连库持久化页面),具体解释我都写在注释里了,请参考User类
package com.sjzcvc.recover
public class User {
private int id
private String username
private String password
public int getId() {
return id
}
public void setId(int id) {
this.id = id
}
public String getUsername() {
return username
}
public void setUsername(String username) {
this.username = username
}
public String getPassword() {
return password
}
public void setPassword(String password) {
this.password = password
}
}
DBUtil类
package com.sjzcvc.recover
import java.sql.*
public class DBUtil {
private final String DRIVER = "oracle.jdbc.driver.OracleDriver"
private final String URL = "jdbc:oracle:thin:@127.0.0.1:1521:orcl"
private final String USERNAME = "scott"
private final String PASSWORD = "tiger"
private Connection conn = null
public Connection getConnection(){
try {
//注册驱动
Class.forName(this.DRIVER)
//获取连接
conn = DriverManager.getConnection(this.URL,this.USERNAME,this.PASSWORD)
} catch (ClassNotFoundException e) {
e.printStackTrace()
} catch (SQLException e) {
e.printStackTrace()
}
return conn
}
public static void main(String args[]){
DBUtil dbUtil = new DBUtil()
Connection conn = dbUtil.getConnection()
}
}
UserManager类
package com.sjzcvc.recover
import java.sql.*
public class UserManager {
public void addUser(User user) {
DBUtil dbUtil = new DBUtil()
Connection conn = null
String sql = "insert into t_user values(?,?,?)"
PreparedStatement pstmt = null
try {
conn = dbUtil.getConnection()
// 准备好要执行sql语句的pstmt
pstmt = conn.prepareStatement(sql)
pstmt.setInt(1, user.getId())
pstmt.setString(2, user.getUsername())
pstmt.setString(3, user.getPassword())
// 执行插入 *** 作
pstmt.executeUpdate()
} catch (SQLException e) {
e.printStackTrace()
} finally {
// 关闭连接释放资源
try {
pstmt.close()
conn.close()
} catch (SQLException e) {
e.printStackTrace()
}
}
}
public User queryUser(String username) {
DBUtil dbUtil = new DBUtil()
Connection conn = null
String sql = "select * from t_user where username=?"
PreparedStatement pstmt = null
ResultSet rs = null
User user = null
try {
conn = dbUtil.getConnection()
// 准备好要执行sql语句的pstmt
pstmt = conn.prepareStatement(sql)
pstmt.setString(1, username)
// 执行查询 *** 作
rs = pstmt.executeQuery()
if(rs.next()){
String password = rs.getString("password")
user = new User()
user.setId(8)
user.setUsername(username)
user.setPassword(password)
}
System.out.println("add user success!!!")
} catch (SQLException e) {
e.printStackTrace()
} finally {
// 关闭连接释放资源
try {
pstmt.close()
conn.close()
} catch (SQLException e) {
e.printStackTrace()
}
}
return user
}
public static void main(String args[]) {
UserManager um = new UserManager()
// User user = new User()
// user.setId(1)
// user.setUsername("张三")
// user.setPassword("999999")
//
// um.addUser(user)
User user = um.queryUser("masanli")
System.out.println(user.getPassword())
}
}
index.jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<html>
<body>
User regist
<form action="add.jsp" method="post">
Username:<input type="text" name="username"/><br>
Password:<input type="password" name="password"/><br>
<input type="submit" value="submit"/>
</form>
</body>
</html>
add.jsp页面
<%@ page language="java" contentType="text/htmlcharset=GB18030"
pageEncoding="GB18030"%>
<%@ page import="com.sjzcvc.recover.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/htmlcharset=GB18030">
<title>Insert title here</title>
</head>
<body>
<%
//获取表单中提交过来的两个参数username和pasword
String username = request.getParameter("username")
String password = request.getParameter("password")
//把两个参数封装到user对象中
User user = new User()
user.setId(2)
user.setUsername(username)
user.setPassword(password)
//调用添加用户的方法
UserManager um = new UserManager()
um.addUser(user)
out.print("add success!!!")
%>
</body>
</html>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)