
下面的代码即可实现(对数据库的 *** 作):
<%@ pagelanguage="java"
contentType="text/html charset=UTF-8"
pageEncoding="UTF-8"
%>
<%@page import="java.sql.*"%>
<center>
<H1> <font color="blue" size="12">管理中心</font></H1>
<HR />
<table width="80%" border="1">
<tr>
<th>ID</th>
<th>书名</th>
<th>作者</th>
<th>价格</th>
<th>删除</th>
</tr>
<%
// 数据库的名字
String dbName = "zap"
// 登录数据库的用户名
String username = "sa"
// 登录数据库的密码
String password = "123"
// 数据库的IP地址,本机可以用 localhost 或者 127.0.0.1
String host = "127.0.0.1"
// 数据库的端口,一般不会修改,默认为1433
int port = 1433
String connectionUrl = "jdbc:sqlserver://" + host + ":" + port + "databaseName=" + dbName + "user=" + username
+ "password=" + password
//
//声明需要使用的资源
Connection con = null
// Statement 记得用完了一定要关闭
Statement stmt = null
// 结果集,记得用完了一定要关闭
ResultSet rs = null
try {
// 注册驱动
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver")
// 获得一个数据库连接
con = DriverManager.getConnection(connectionUrl)
String SQL = "SELECT * from note"
// 创建查询
stmt = con.createStatement()
// 执行查询,拿到结果集
rs = stmt.executeQuery(SQL)
while (rs.next()) {
%>
<tr>
<td>
<%=rs.getInt(1)%>
</td>
<td>
<a href="prepareupdate?ID=<%=rs.getInt("ID")%>" target="_blank"><%=rs.getString(2)%></a>
</td>
<td>
<%=rs.getString(3)%>
</td>
<td>
<%=rs.getString(4)%>
</td>
<td>
<a href="delete?ID=<%=rs.getInt("ID")%>" target="_blank">删除</a>
</td>
</tr>
<%
}
} catch (Exception e) {
// 捕获并显示异常
e.printStackTrace()
} finally {
// 关闭我们使用过的资源
if (rs != null)
try {
rs.close()
} catch (Exception e) {}
if (stmt != null)
try {
stmt.close()
} catch (Exception e) {}
if (con != null)
try {
con.close()
} catch (Exception e) {}
}
%>
</table>
<a href="insert.jsp">添加新纪录</a>
</center>
问题太抽象,/** To change this template, choose Tools | Templates
* and open the template in the editor.
*/package ******
import java.io.PrintWriter
import java.sql.*/**
* @author wfg
*/
public class DB_Conn {
private String driverName = "com.mysql.jdbc.Driver" //JDBC驱动
private String userName = "root" //数据库用户名
private String userPwd = "*****" //数据库用户密码
private String dbName = "******"//数据库名
private String url = "jdbc:mysql://localhost:3306/"+dbName+"?user="+userName+
"&password="+userPwd //数据库连接字符串
private Connection conn = null//数据库连接对象
public Statement sm = null//数据库语句对象
private PrintWriter out = null //建立数据库连接函数
public void ConnectDB(){
try{
Class.forName(driverName).newInstance()
conn = DriverManager.getConnection(url)
sm = conn.createStatement()
}
catch(Exception e){
e.printStackTrace()
out.print("数据库连接失败!")
}
}//释放数据库连接函数
public void CloseDB(){
try{
if(sm != null){
sm.close()
}
conn.close()
}
catch(SQLException SqlE){
SqlE.printStackTrace()
out.print("数据库关闭失败!")
}
}
}
这是先建立连接
还有这样的需求呀?无奇不有……写的dao的实现就行了呀 ,何必得要jsp呢?用jsp,只会越来越乱^
CustomerDao.java
package cn.itcast.dao
import java.util.List
import cn.itcast.domain.Customer
public interface CustomerDao {
public void add(Customer customer)
public Customer find(int id)
public List getAllCustomer()
public void delete(int id)
public void update(Customer customer)
public int getAllRecord()
public List getCustomerByPage(int startindex,int pagesize)
}
CustomerDaoJdbcImpl.java
package cn.itcast.dao.impl
import java.sql.Connection
import java.sql.PreparedStatement
import java.sql.ResultSet
import java.util.ArrayList
import java.util.List
import cn.itcast.dao.CustomerDao
import cn.itcast.domain.Customer
import cn.itcast.util.JdbcUtils
public class CustomerDaoJdbcImpl implements CustomerDao {
/*
id int primary key auto_increment,
name varchar(20) not null,
sex varchar(4) not null,
birthday date,
cellphone varchar(20) not null,
Email varchar(40),
preference varchar(100),
type varchar(40),
Description varchar(255)
*/
public void add(Customer customer) {
Connection conn = null
PreparedStatement st = null
ResultSet rs = null
try{
conn = JdbcUtils.getConnection()
String sql = "insert into customer(name,sex,birthday,cellphone,email,preference,type,description) values(?,?,?,?,?,?,?,?)"
st = conn.prepareStatement(sql)
st.setString(1, customer.getName())
st.setString(2, customer.getSex())
st.setDate(3, new java.sql.Date(customer.getBirthday().getTime()))
st.setString(4, customer.getCellphone())
st.setString(5, customer.getEmail())
st.setString(6, customer.getPreference())
st.setString(7, customer.getType())
st.setString(8, customer.getDescription())
st.executeUpdate()
}catch(Exception e){
throw new RuntimeException(e)
}finally{
JdbcUtils.release(rs, st, conn)
}
}
public void delete(int id) {
Connection conn = null
PreparedStatement st = null
ResultSet rs = null
try{
conn = JdbcUtils.getConnection()
String sql = "delete from customer where id=?"
st = conn.prepareStatement(sql)
st.setInt(1, id)
st.executeUpdate()
}catch(Exception e){
throw new RuntimeException(e)
}finally{
JdbcUtils.release(rs, st, conn)
}
}
public Customer find(int id) {
Connection conn = null
PreparedStatement st = null
ResultSet rs = null
try{
conn = JdbcUtils.getConnection()
String sql = "select id,name,sex,birthday,cellphone,email,preference,type,description from customer where id=?"
st = conn.prepareStatement(sql)
st.setInt(1, id)
rs = st.executeQuery()
if(rs.next()){
Customer c = new Customer()
c.setId(rs.getInt("id"))
c.setName(rs.getString("name"))
c.setSex(rs.getString("sex"))
c.setBirthday(rs.getDate("birthday"))
c.setCellphone(rs.getString("cellphone"))
c.setEmail(rs.getString("email"))
c.setPreference(rs.getString("preference"))
c.setType(rs.getString("type"))
c.setDescription(rs.getString("description"))
return c
}
return null
}catch(Exception e){
throw new RuntimeException(e)
}finally{
JdbcUtils.release(rs, st, conn)
}
}
/*
Id 编号 varchar(20)
name 客户姓名 varchar(20)
sex 性名 varchar(4)
birthday 生日 date
cellphone 手机 varchar(20)
Email 电子邮件 varchar(40)
preference 客户爱好 varchar(100)
type 客户类型 varchar(40)
Description 备注 varchar(255)
*/
public List getAllCustomer() {
Connection conn = null
PreparedStatement st = null
ResultSet rs = null
try{
conn = JdbcUtils.getConnection()
String sql = "select id,name,sex,birthday,cellphone,email,preference,type,description from customer order by id"
st = conn.prepareStatement(sql)
rs = st.executeQuery()
List list = new ArrayList()
while(rs.next()){
Customer c = new Customer()
c.setId(rs.getInt("id"))
c.setName(rs.getString("name"))
c.setSex(rs.getString("sex"))
c.setBirthday(rs.getDate("birthday"))
c.setCellphone(rs.getString("cellphone"))
c.setEmail(rs.getString("email"))
c.setPreference(rs.getString("preference"))
c.setType(rs.getString("type"))
c.setDescription(rs.getString("description"))
list.add(c)
}
return list
}catch(Exception e){
throw new RuntimeException(e)
}finally{
JdbcUtils.release(rs, st, conn)
}
}
public void update(Customer customer) {
Connection conn = null
PreparedStatement st = null
ResultSet rs = null
try{
conn = JdbcUtils.getConnection()
String sql = "update customer set name=?,sex=?,birthday=?,cellphone=?,email=?,preference=?,type=?,description=? where id=?"
st = conn.prepareStatement(sql)
st.setString(1, customer.getName())
st.setString(2, customer.getSex())
st.setDate(3, new java.sql.Date(customer.getBirthday().getTime()))
st.setString(4, customer.getCellphone())
st.setString(5, customer.getEmail())
st.setString(6, customer.getPreference())
st.setString(7, customer.getType())
st.setString(8, customer.getDescription())
st.setInt(9, customer.getId())
st.executeUpdate()
}catch(Exception e){
throw new RuntimeException(e)
}finally{
JdbcUtils.release(rs, st, conn)
}
}
public int getAllRecord() {
Connection conn = null
PreparedStatement st = null
ResultSet rs = null
try{
conn = JdbcUtils.getConnection()
String sql = "select count(*) from customer"
st = conn.prepareStatement(sql)
rs = st.executeQuery()
if(rs.next()){
return rs.getInt(1)
}
return 0
}catch(Exception e){
throw new RuntimeException(e)
}finally{
JdbcUtils.release(rs, st, conn)
}
}
public List getCustomerByPage(int startindex, int pagesize) {
Connection conn = null
PreparedStatement st = null
ResultSet rs = null
try{
conn = JdbcUtils.getConnection()
String sql = "select id,name,sex,birthday,cellphone,email,preference,type,description from customer limit ?,?"
st = conn.prepareStatement(sql)
st.setInt(1, startindex)
st.setInt(2, pagesize)
rs = st.executeQuery()
List list = new ArrayList()
while(rs.next()){
Customer c = new Customer()
c.setId(rs.getInt("id"))
c.setName(rs.getString("name"))
c.setSex(rs.getString("sex"))
c.setBirthday(rs.getDate("birthday"))
c.setCellphone(rs.getString("cellphone"))
c.setEmail(rs.getString("email"))
c.setPreference(rs.getString("preference"))
c.setType(rs.getString("type"))
c.setDescription(rs.getString("description"))
list.add(c)
}
return list
}catch(Exception e){
throw new RuntimeException(e)
}finally{
JdbcUtils.release(rs, st, conn)
}
}
}
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)