java对数据库的增删改查,急用java写一个 连接数据库 增删改的例子
各位老铁们,大家好,今天由我来为大家分享java对数据库的增删改查,以及急用java写一个 连接数据库 增删改的例子的相关问题知识,希望对大家有所帮助。如果可以帮助到大家,还望关注收藏下本站,您的支持是我们最大的动力,谢谢大家了哈,下面我们开始吧!
急用java写一个 连接数据库 增删改的例子
DBConnectionManager.java//连接数据库用的
import java.sql.*;
public class DBConnectionManager{
private String driverName="com.microsoft.jdbc.sqlserver.SQLServerDriver";
private String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=demo";
private String user="sa";
private String password="";
public String getDriverName(){
return driverName;
}
public void setDriverName(String driverName){
this.driverName= driverName;
}
public String getPassword(){
return password;
}
public void setPassword(String password){
this.password= password;
}
public String getUrl(){
return url;
}
public void setUrl(String url){
this.url= url;
}
public String getUser(){
return user;
}
public void setUser(String user){
this.user= user;
}
public Connection getConnection(){
try{
Class.forName(driverName);
return DriverManager.getConnection(url, user, password);
}catch(Exception e){
e.printStackTrace();
return null;
}
}
}
DBSQLManager.java//操作数据库用的
import java.sql.*;
public class DBSQLManager{
protected Connection con=null;//Connection对象
protected Statement stmt=null;//Statement对象
protected ResultSet rs=null;//记录结果集
protected String sql="";//SQL语句
public DBSQLManager(){
try{
DBConnectionManager dcm=new DBConnectionManager();
con=dcm.getConnection();
//con.setAutoCommit(false);//添加事物,既是否自动提交
stmt=con.createStatement();
} catch(SQLException e){
e.printStackTrace();
}
}
public Statement getStmt(){
return stmt;
}
public Connection getCon(){
return con;
}
public ResultSet getRs(){
return rs;
}
public void setSql(String sql){
this.sql=sql;
}
public String getSql(){
return sql;
}
//查找
public void execueQuery(){
try{
rs=stmt.executeQuery(sql);
} catch(SQLException e){
e.printStackTrace();
}
}
//更新
public void executeUpdate(){
try{
stmt.executeUpdate(sql);
} catch(SQLException e){
e.printStackTrace();
}
}
//关闭
public void close(){
if(rs!=null){
try{
rs.close();
} catch(SQLException e){
e.printStackTrace();
}
rs=null;
}
if(stmt!=null){
try{
stmt.close();
} catch(SQLException e){
e.printStackTrace();
}
stmt=null;
}
try{
con.close();
} catch(SQLException e){
e.printStackTrace();
}
con=null;
}
}
SqlOperate.java//用来调用数据库操作语句
import java.sql.ResultSet;
import java.sql.SQLException;
public class SqlOperate{
//插入,修改,删除
public void insOrModOrDel(String sql){
DBSQLManager dbsm=new DBSQLManager();
dbsm.getStmt();
dbsm.setSql(sql);
dbsm.executeUpdate();
dbsm.close();
}
//显示
public void display(String sql){
DBSQLManager dbsm=new DBSQLManager();
dbsm.getStmt();
dbsm.setSql(sql);
dbsm.execueQuery();
ResultSet rs=dbsm.getRs();
try{
while(rs!=null&&rs.next()){
System.out.print(rs.getObject(1)+"\t");
System.out.print(rs.getObject(2)+"\t");
System.out.print(rs.getObject(3)+"\t");
System.out.println(rs.getObject(4));
}
dbsm.close();
} catch(SQLException e){
e.printStackTrace();
}
}
}
SqlMain.java//数据库的测试主函数
public class SqlMain{
public static void main(String[] args){
SqlOperate sqlOpt=new SqlOperate();
// sqlOpt.insOrModOrDel("insert into user1 values('qianhaifei',999999,'qianhaifei@163.com')");//插入
// sqlOpt.insOrModOrDel("update user1 set username='weixiangyang' where id=4");//修改
// sqlOpt.insOrModOrDel("delete from user1 where username='weixy2000'");//删除
sqlOpt.display("select*from user1");//显示
}
}
楼主用的时间只需要将数据库用户名和密码还有数据库表名改一下就行了...
其他都是一样的...
楼主会改吧.../???
如果有什么问题的话请百度HI我...帮你解决....
如果楼主要我的数据库的话....请百度HI我...我给你就行了...
祝楼主早日成功!
如何使用java对oracle数据库进行增删查改
用eclipse
首先在oracle文件下找到jdbc文件,里面的lib文件下有个class12.zip
在eclipse里新建个项目,建好后在左边右击那个项目图标,选择属性(最下面的),里面有个java build path,点进去,找到add external jars按键,点一下,把那个class12.zip加载进来,然后可以开始写代码了。
我的代码如下:
import java.sql.*;
public class connect{
/**
*@param args
*/
public static void main(String[] args){
// TODO Auto-generated method stub
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
}catch(ClassNotFoundException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
Connection con= null;
Statement stmt= null;
ResultSet rs= null;
try{
String url="jdbc:oracle:thin:@服务器ip地址:端口号:数据库名字";
String user="用户名";
String password="密码";
String str="INSERT INTO ZZZ_2 VALUES('041110018','JHDK')";
con= java.sql.DriverManager.getConnection(url,user,password);
//创建状态
stmt= con.createStatement();
//执行SQL语句,返回结果集
//int rowcount= stmt.executeUpdate(str);
int j= stmt.executeUpdate("update ZZZ_2 set NAME='dbt' where ID=21");
int k= stmt.executeUpdate("delete from ZZZ_2 where ID=41110020");
rs= stmt.executeQuery("SELECT* FROM ZZZ_2");
//对结果集进行处理
while(rs.next()){
int id= rs.getInt("ID");
String name= rs.getString("NAME");
//Integer age= rs.getObject("age")== null? null: rs.getInt("age");
System.out.println(id+":"+ name);
}}catch(SQLException e){
e.printStackTrace();}
//释放资源
finally{
try{
rs.close();
}catch(SQLException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
try{
stmt.close();
}catch(SQLException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
try{
con.close();
}catch(SQLException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
注意,上面的ip地址什么的要填对,ZZZ_2是我随便建的表,你改成自己的表名就行了
java io 简单实现对数据的增删改查
//以下是个简单例子自己可以扩展分别可以作为一方法
File file= new File("D:/e.txt");
try{
if(!file.exists()){
file.createNewFile();
}
//添加
FileOutputStream fileOutputStream= new FileOutputStream(file, true);
fileOutputStream.write("[id=1,userName=管理员,passWord=admin]\r\n"
.getBytes("UTF-8"));//格式自由定义
fileOutputStream.close();
//修改 id=1
int id= 1;
String userName="周述兵";
String passWord="zsb";
//用输入流读取数据也就是查询
FileInputStream fileInputStream= new FileInputStream(file);
byte[] bytes= new byte[fileInputStream.available()];
fileInputStream.read(bytes);
String[] datas= new String(bytes,"UTF-8").split("\r\n");//根据每天数据间的区别来划分为数组
for(int i= 0; i< datas.length; i++){
System.out.println(datas[i]);
if(datas[i].indexOf("[id="+ id)>= 0){
datas[i]="[id="+ id+",userName="+ userName
+",passWord="+ passWord+"]";
}
}
fileInputStream.close();
//用输出流重新写入数据
fileOutputStream= new FileOutputStream(file, false);
String writeData="";
for(String data: datas){
writeData+= data+"\r\n";
}
fileOutputStream.write(writeData.getBytes("UTF-8"));
fileOutputStream.close();
//删除和修改有所略同
} catch(Exception e){
e.printStackTrace();
}
//如果考虑效率的话我建议不要用 io因为这最消耗性能了你可以考虑存入多个文件分类存入分类查找速度稍微好点
最好还是用数据库
java对数据库的增删改查的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于急用java写一个 连接数据库 增删改的例子、java对数据库的增删改查的信息别忘了在本站进行查找哦。