JSP中如何对数据库中的数据进行删除增加等操作
4个回答
2013-10-18
展开全部
问题太抽象,/*
* 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("数据库关闭失败!");
}
}
}
这是先建立连接
* 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("数据库关闭失败!");
}
}
}
这是先建立连接
2013-10-18
展开全部
你说的是用JAVA来对数据库中数据来进行删增操作是吧。JSP的话我就帮不了你啦,
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-10-18
展开全部
通过后台执行sql语句来实现。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-10-18
展开全部
package cn.com.csuinfo.work.util;import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Vector;/**
* 完成基本的增删改查功�1�7?,�1�7?有的dao都必须继承该父类
*
* @author lhb
*
*/
public class ORCZFBasicDao {
private DBConnection db = DBConnection.getDBConnection(); /**
* 数据库的修改操作(insert,update,delete)
*
* @param executeSql
* :要操作的sql语句;
* @param paramValues
* :动�1�7?�1�7sql中的参数列表�1�7?
*/
public boolean updateZfSystem(String sql, Object[] values) throws Exception {
boolean flag = false;// 默认为操作失�1�7?
Connection conn = null;
PreparedStatement pstmt = null; try {
conn = db.getConnection();
pstmt = conn.prepareStatement(sql);
// 判断执行的sql是否有参�1�7?
if (values != null) {
int i = 1;
for (Object value : values) { pstmt.setObject(i++, value);
}
}
int row = pstmt.executeUpdate();
if (row > 0) {
flag = true;
}
} catch (Exception ex) {
ex.printStackTrace();
flag = false;
} finally {
db.closeDBResource(conn, pstmt, null);
return flag;
}
} /**
* �0�1�1�7�1�7�0�9�0�5�1�7�1�7
*/
public Vector queryZfSystem(String str, Object[] values)
throws Exception {
Vector vector = new Vector(); ResultSet result = null;
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = db.getConnection();
pstmt = conn.prepareStatement(str);
// 判断执行的sql是否有参�1�7?
if (values != null) {
int i = 1;
for (Object value : values) {
pstmt.setObject(i++, value);
}
}
result = pstmt.executeQuery();
while(result.next()) {
vector.add(result.getString(1));
} } catch (Exception ex) {
ex.printStackTrace(); } finally { return vector;
} } public Vector<String> doSelectOne(String sql, Object[] values, int column) { Vector inFoVector=null;
Connection conn = db.getConnection();
Statement st = null;
ResultSet rs = null;
PreparedStatement pstmt = null;
try {
conn = db.getConnection();
pstmt = conn.prepareStatement(sql);
// 判断执行的sql是否有参�1�7?
if (values != null) {
int i = 1;
for (Object value : values) {
pstmt.setObject(i++, value);
}
}
rs = pstmt.executeQuery(); if (rs.next()) {
inFoVector = new Vector();
for (int i = 1; i <= column; i++) {
inFoVector.add(rs.getString(i));
} } else {
System.out.println(rs == null);
} } catch (Exception e) { } finally {
db.closeDBResource(conn, pstmt, rs);
}
return inFoVector;
}
}
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Vector;/**
* 完成基本的增删改查功�1�7?,�1�7?有的dao都必须继承该父类
*
* @author lhb
*
*/
public class ORCZFBasicDao {
private DBConnection db = DBConnection.getDBConnection(); /**
* 数据库的修改操作(insert,update,delete)
*
* @param executeSql
* :要操作的sql语句;
* @param paramValues
* :动�1�7?�1�7sql中的参数列表�1�7?
*/
public boolean updateZfSystem(String sql, Object[] values) throws Exception {
boolean flag = false;// 默认为操作失�1�7?
Connection conn = null;
PreparedStatement pstmt = null; try {
conn = db.getConnection();
pstmt = conn.prepareStatement(sql);
// 判断执行的sql是否有参�1�7?
if (values != null) {
int i = 1;
for (Object value : values) { pstmt.setObject(i++, value);
}
}
int row = pstmt.executeUpdate();
if (row > 0) {
flag = true;
}
} catch (Exception ex) {
ex.printStackTrace();
flag = false;
} finally {
db.closeDBResource(conn, pstmt, null);
return flag;
}
} /**
* �0�1�1�7�1�7�0�9�0�5�1�7�1�7
*/
public Vector queryZfSystem(String str, Object[] values)
throws Exception {
Vector vector = new Vector(); ResultSet result = null;
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = db.getConnection();
pstmt = conn.prepareStatement(str);
// 判断执行的sql是否有参�1�7?
if (values != null) {
int i = 1;
for (Object value : values) {
pstmt.setObject(i++, value);
}
}
result = pstmt.executeQuery();
while(result.next()) {
vector.add(result.getString(1));
} } catch (Exception ex) {
ex.printStackTrace(); } finally { return vector;
} } public Vector<String> doSelectOne(String sql, Object[] values, int column) { Vector inFoVector=null;
Connection conn = db.getConnection();
Statement st = null;
ResultSet rs = null;
PreparedStatement pstmt = null;
try {
conn = db.getConnection();
pstmt = conn.prepareStatement(sql);
// 判断执行的sql是否有参�1�7?
if (values != null) {
int i = 1;
for (Object value : values) {
pstmt.setObject(i++, value);
}
}
rs = pstmt.executeQuery(); if (rs.next()) {
inFoVector = new Vector();
for (int i = 1; i <= column; i++) {
inFoVector.add(rs.getString(i));
} } else {
System.out.println(rs == null);
} } catch (Exception e) { } finally {
db.closeDBResource(conn, pstmt, rs);
}
return inFoVector;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |