java连接数据库增删改查(在eclipse下 怎样使用java.jsp连接数据库,实现数据库的增删改查)

本文目录
- 在eclipse下 怎样使用java.jsp连接数据库,实现数据库的增删改查
- java web 中连接数据库并在HTML上进行增删改查
- java jdbc连接数据库 oracle的代码实现增删改查的方法怎么写
- 在Java中如何对数据库中的数据进行操作
- 编写一个java程序,通过jdbc访问数据库实现对数据库的插入,删除,修改和查询操作
- JAVA 实现数据库增删改查的Dao和DaoImpl的写法
在eclipse下 怎样使用java.jsp连接数据库,实现数据库的增删改查
新建一个.jsp页面,然后清空,里面写
《%
链接数据库的代码
%》
在工程里调用这个页面就行了
java web 中连接数据库并在HTML上进行增删改查
ODBC 连接数据库也不分J2EE ,JSEE啊?????
无论是J2EE还是JSEE都可以用ODBC连接数据库啊。
区别是有的,
J2EE是做Web程序的,他面对的是大量的用户同时访问服务器而且可能同时操作数据库,如果你使用ODBC连接数据库,所有的用户都用同一个连接,频繁的创建销毁资源,这样导致速度很慢,效率低,所以WEB开发一半采用连接池技术,用架构做,现在常用的是Hibernate。
我要是没记错的话
JSEE开发桌面应用程序多,这样可能同时操作的人少,一般一个桌面程序也就一个人在操作,所以用ODBC也就足够了,可是虽然解决了效率的问题但是没有解决面向对象与面向关系的良好沟通。
J2EE ,JSEE后台的数据库连接技术是通用的不分j2ee和JSEE吧
java jdbc连接数据库 oracle的代码实现增删改查的方法怎么写
修改和删除,可以使用ResultSet直接修改,切换到状态就可以。。。。。。。
建议直接使用SQL去修改,特别是删除,使用SQL很快捷
在Java中如何对数据库中的数据进行操作
package com.dao;import java.sql.*;import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;public class BaseDao {
/**
* 创建数据库连接及关闭
*/
// 打开连接
public static Connection getConnection() {
Connection con = null; /*************************** oracl 的连接 ***************************************/
// try { // Class.forName("oracle.jdbc.driver.OracleDriver");
// con = DriverManager.getConnection(
// "jdbc:oracle:thin:@127.0.0.1:1521:orcl", "bbs", "sa");
// } catch (ClassNotFoundException e) {
// e.printStackTrace();
// } catch (SQLException e) {
// e.printStackTrace();
// }
/******************************* sqlerver 的连接 ******************************/
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection(
"jdbc:sqlserver://127.0.0.1:1433;databasename=bbs", "sa",
"zhou");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
/*********************************************************************/
return con;
} // 关闭
public static void closeAll(Connection connection,
PreparedStatement pStatement, ResultSet res) {
try {
if (connection != null && (!connection.isClosed())) {
connection.close();
}
if (res != null) {
res.close();
res = null;
}
if (pStatement != null) {
pStatement.close();
pStatement = null;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
对数据库增删改查package com.dao;import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;import com.entity.News;public class NewsDao {
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
/**
* 添加新闻
* @param news
* @return
*/
public boolean newsAdd(News news){
boolean result=false;
String sql="insert into news values(?,?)";
con=BaseDao.getConnection();
try {
pstmt=con.prepareStatement(sql);
pstmt.setString(1, news.getContent());
pstmt.setString(2, FormatTime.newTime());
int i = 0;
i = pstmt.executeUpdate();
if (i 》 0)
result = true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
/**
* 修改新闻
* @param news
* @return
*/
public boolean updateNews(News news){
boolean result=false;
con=BaseDao.getConnection();
try {
pstmt=con.prepareStatement("update news set content=? ,writedate=? where newsid=?");
pstmt.setString(1, news.getContent());
pstmt.setString(2, FormatTime.newTime());
pstmt.setInt(3, news.getNewsID());
int i = 0;
i = pstmt.executeUpdate();
if (i 》 0)
result = true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
/**
* 删除新闻
* @param news
* @return
*/
public boolean deleteNews(News news){
boolean result=false;
String sql=String.format("delete from news where newsid=%d", news.getNewsID());
con=BaseDao.getConnection();
try {
pstmt=con.prepareStatement(sql);
int i = 0;
i = pstmt.executeUpdate();
if (i 》 0)
result = true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
/**
* 删除新闻 重载
* @param newsId
* @return
*/
public boolean deleteNews(int newsId){
boolean result=false;
String sql=String.format("delete from news where newsid=%d", newsId);
con=BaseDao.getConnection();
try {
pstmt=con.prepareStatement(sql);
int i = 0;
i = pstmt.executeUpdate();
if (i 》 0)
result = true;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
/**
* 查询所有的新闻
* @return
*/
public List《News》 selectAllNews(){
List《News》 list=new ArrayList《News》();
String sql="select * from Users";
con=BaseDao.getConnection();
try {
pstmt=con.prepareStatement(sql);
rs=pstmt.executeQuery();
while(rs.next()){
News news=new News();
news.setNewsID(rs.getInt(1));
news.setContent(rs.getString(2));
news.setWriteDate(rs.getString(3));
list.add(news);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
BaseDao.closeAll(rs, pstmt, con);
}
return list;
}
/**
* 查询单个
* @return
*/
public News selectOneNews(){
News news=new News();
con=BaseDao.getConnection();
try {
pstmt=con.prepareStatement("select top 1 * from news order by newsid desc");
rs=pstmt.executeQuery();
while(rs.next()){
news.setNewsID(rs.getInt(1));
news.setContent(rs.getString(2));
news.setWriteDate(rs.getString(3));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
BaseDao.closeAll(rs, pstmt, con);
}
return news;
}
}
实体类package com.entity;import java.io.Serializable;
public class News implements Serializable{
private int newsID;
private String content;
private String writeDate; public News() {
super();
// TODO Auto-generated constructor stub
} public News(String content, String writeDate) {
super();
this.content = content;
this.writeDate = writeDate;
} public News(int newsID, String content, String writeDate) {
super();
this.newsID = newsID;
this.content = content;
this.writeDate = writeDate;
} public int getNewsID() {
return newsID;
} public void setNewsID(int newsID) {
this.newsID = newsID;
} public String getContent() {
return content;
} public void setContent(String content) {
this.content = content;
} public String getWriteDate() {
return writeDate;
} public void setWriteDate(String writeDate) {
this.writeDate = writeDate;
}
}
编写一个java程序,通过jdbc访问数据库实现对数据库的插入,删除,修改和查询操作
我刚写了一个只有插入的,望采纳
import java.sql.*;
import java.util.*;
public class TestPre {
public static void main(String args) {
int i=0,deptno=0;//i只做while循环使用,deptno是表dept2中的一个属性,类型是int
String dname=null,loc=null;//dname和loc也是表dept2的属性,类型是String
Scanner s=new Scanner(System.in);
System.out.println("请输入3个参数");
while(i《3){
try{
deptno=s.nextInt();
i++;
dname=s.next();
i++;
loc=s.next();
i++;
}catch(InputMismatchException e){
System.out.println("输入的类型不符,退出");
System.exit(-1);
}
}
Connection conn=null;
PreparedStatement pstmt=null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost/mydata?"+ "user=root&password=root");
pstmt=conn.prepareStatement("insert into dept2 values(?,?,?)");
pstmt.setInt(1, deptno);
pstmt.setString(2, dname);
pstmt.setString(3, loc);
pstmt.executeUpdate();
System.out.println("插入完成");
} catch (ClassNotFoundException e) {
System.out.println("连接数据库不成功,程序退出");
System.exit(-1);
} catch (SQLException e) {
System.out.println("连接数据库不成功,程序退出");
System.exit(-1);
}
finally{
try{
if(pstmt!=null){
pstmt.close();
pstmt=null;
}
if(conn!=null){
conn.close();
conn=null;
}
}catch(SQLException e){
e.printStackTrace();
}
}
}
}
JAVA 实现数据库增删改查的Dao和DaoImpl的写法
package org.dao;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Date;
import org.dbc.DBManageer;
import org.dbc.DBUtil;
import org.vo.CardInfo;
public class CardInfoDao {
public void testResultSetMetaData() {
String sql = "select * from cardinfo";
Connection con = new DBUtil().getConnection();
PreparedStatement ps = null;
ResultSetMetaData rsmd = null;
ResultSet rs = null;
try {
ps = con.prepareStatement(sql);
rs = ps.executeQuery();
rsmd = rs.getMetaData();
for (int i = 1; i 《 rsmd.getColumnCount(); i++) {
System.out.print(" 数据类型名:" + rsmd.getColumnClassName(i));
System.out.print(" 别名:" + rsmd.getColumnLabel(i));
System.out.print(" 列名:" + rsmd.getColumnName(i));
System.out.print(" 数据类型:" + rsmd.getColumnTypeName(i));
System.out.println(" 数据类型:" + rsmd.getColumnType(i));
}
while (rs.next()) {
// 通过反射可以对VO对象(CardInfo)自动赋值
// for(...)
// CardInfo ci=new CardInfo();
// ci.setId(rs.getInt(columnIndex))
/*
* CardInfo ca = new CardInfo(); Class cc = ca.getClass();
*/
/*Class《?》 c = Class.forName("org.vo.CardInfo");
CardInfo ca = (CardInfo) c.newInstance();
ca.setCardId(rs.getString("cardId"));
ca.setCustomerName(rs.getString("customerName"));
ca.setCurrentMoney(rs.getFloat("currentMoney"));
ca.setOpenDate(rs.getDate("openDate"));
System.out.println(ca);//ok */
//加载一个CardInfo类
Class c = Class.forName("org.vo.CardInfo");
Object o = c.newInstance(); //获得它的一个实例
//定义String类的对象数组
Class { String.class};
//定义float类的对象数组
Class { float.class};
//定义Date(util)类的对象数组
Class { Date.class};
//获得setCardId方法
Method me = c.getMethod("setCardId", params);
//实例方法的参数
Object {rs.getString("cardId")};
//如果底层方法是静态的,那么可以忽略指定的 obj 参数。该参数可以为 null。
//如果底层方法所需的形参数为 0,则所提供的 args 数组长度可以为 0 或 null。
//o 调用方法的对象 ostr 方法调用的参数
me.invoke(o, ostr);
//获得setCustomerName方法
Method name = c.getMethod("setCustomerName", params);
Object {rs.getString("customerName")};
name.invoke(o,cusname);
//获得setCurrentMoney方法
Method money = c.getMethod("setCurrentMoney", floatparams);
Object {rs.getFloat("currentMoney")};
money.invoke(o,cusmoney);
//获得setOpenDate方法
Method date = c.getMethod("setOpenDate",dateparams);
Object {rs.getDate("openDate")};
date.invoke(o, openDate);
//打印
System.out.println(o);
}
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
}catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
public void showResultSet() {
String sql = "select * from cardinfo";
Connection con = new DBManageer().getConnection();
PreparedStatement ps = null;
try {
ps = con.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
ResultSet rs = ps.executeQuery();
System.out.println("---依次读取------");
while (rs.next()) {
CardInfo ca = new CardInfo();
ca.setCardId(rs.getString("cardId"));
ca.setCustomerName(rs.getString("customerName"));
ca.setCurrentMoney(rs.getFloat("currentMoney"));
ca.setOpenDate(rs.getDate("openDate"));
System.out.println(ca);
}
System.out.println("---倒读------");
while (rs.previous()) {
CardInfo ca = new CardInfo();
ca.setCardId(rs.getString("cardId"));
ca.setCustomerName(rs.getString("customerName"));
ca.setCurrentMoney(rs.getFloat("currentMoney"));
ca.setOpenDate(rs.getDate("openDate"));
System.out.println(ca);
}
rs.absolute(3);// 定位倒第几行
rs.updateString("customerName", "star");
// rs.updateRow();
rs.beforeFirst();
while (rs.next()) {
CardInfo ca = new CardInfo();
ca.setCardId(rs.getString("cardId"));
ca.setCustomerName(rs.getString("customerName"));
ca.setCurrentMoney(rs.getFloat("currentMoney"));
ca.setOpenDate(rs.getDate("openDate"));
System.out.println(ca);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}

更多文章:
手机老是出现failed(华为手机errconnectfailed)
2025年12月31日 05:30
mathematica10下载(mathematica免费下载地址)
2026年1月12日 23:00
catchphrase的意思(catchphrase是什么意思)
2025年7月19日 18:45
server作主语(英语you must set two server name values怎么翻译)
2026年2月25日 04:00
c语言求最大公约数和最小公倍数辗转相除法(c语言求最大公约数和最小公倍数)
2025年6月24日 13:30
表单大师下载表单大师app下载(眉山市幼儿报名登记表怎么下载下来)
2025年7月1日 03:15
count函数怎么用来统计单元格数量(EXCEL中如何统计此列有数字的单元格个数)
2025年9月23日 17:15
eclipse jdk编译版本(JDK7搭配什么版本的eclipse若是jdk 1.7的话该用Eclipse的哪个版本)
2026年1月15日 13:30
模板制作检验批多少米一张(3000多米的围墙,每100米做一个检验批具体怎么做)
2026年9月17日 02:30
easyuidatagrid 获取选中行数据到详情页(easyui datagrid 怎样获取点击行的数据)
2025年6月21日 20:00
img格式打开(win7电脑如何打开img文件win7系统打开img文件的方法)
2025年9月24日 08:30
befarform是什么意思(be far form中的form什么意思 说详细些)
2025年12月14日 23:15
两函数相加定义域怎么求(两函数相加 定义区怎么求啊拜托了各位 谢谢)
2026年9月20日 15:30












