评论

收藏

[Java] Java基于jdbc实现的增删改查操作示例

编程语言 编程语言 发布于:2021-10-05 18:00 | 阅读数:537 | 评论:0

这篇文章主要介绍了Java基于jdbc实现的增删改查操作,结合实例形式分析了java使用jdbc进行数据库的连接、增删改查等基本操作技巧,需要的朋友可以参考下
本文实例讲述了java基于jdbc实现的增删改查操作。分享给大家供大家参考,具体如下:
增删改操作:
package java_web;
import java.sql.drivermanager;
import java.sql.sqlexception;
import com.mysql.jdbc.connection;
import com.mysql.jdbc.statement;
/**
* jdbc curd
* @author administrator
*
*/
public class rbacdemo {
public final static string url="jdbc:mysql://localhost:3306/test";
public final static string username="root";
public final static string password="";
public final static string driver="com.mysql.jdbc.driver";
/**
* 插入
*/
public static void insert(){
try {
class.forname(driver);
connection conn = (connection) drivermanager.getconnection(url,username,password);
string sql = "insert into test(name,sex)values('fifi2',1),('fifi3',3)";
statement state = (statement) conn.createstatement();
int result=state.executeupdate(sql);
state.close();
conn.close();
system.out.println(result+"success");
} catch (classnotfoundexception e) {
// todo 自动生成的 catch 块
e.printstacktrace();
} catch (sqlexception e) {
// todo 自动生成的 catch 块
e.printstacktrace();
}
}
public static void update(){
try {
class.forname(driver);
connection conn = (connection) drivermanager.getconnection(url,username,password);
string sql = "update test set name='fifi3aaa' where name='fifi3'";
statement state = (statement) conn.createstatement();
int result=state.executeupdate(sql);
state.close();
conn.close();
system.out.println(result+"success");
} catch (classnotfoundexception e) {
// todo 自动生成的 catch 块
e.printstacktrace();
} catch (sqlexception e) {
// todo 自动生成的 catch 块
e.printstacktrace();
}
}
public static void delete(){
try {
class.forname(driver);
connection conn = (connection) drivermanager.getconnection(url,username,password);
string sql = "delete from test where name='fifi3aaa'";
statement state = (statement) conn.createstatement();
int result=state.executeupdate(sql);
state.close();
conn.close();
system.out.println(result+"success");
} catch (classnotfoundexception e) {
// todo 自动生成的 catch 块
e.printstacktrace();
} catch (sqlexception e) {
// todo 自动生成的 catch 块
e.printstacktrace();
}
}
public static void main(string[] args){
//insert();
//update();
delete();
}
}
查询操作:
package java_web;
import java.sql.drivermanager;
import java.sql.resultset;
import java.sql.sqlexception;
import com.mysql.jdbc.connection;
import com.mysql.jdbc.statement;
public class jdbcquerydemo {
public final static string url="jdbc:mysql://localhost:3306/test";
public final static string username="root";
public final static string password="";
public final static string driver="com.mysql.jdbc.driver";
public static void query(){
try {
class.forname(driver);
connection conn = (connection) drivermanager.getconnection(url,username,password);
string sql = "select id,name,sex from test where id=3";
statement state = (statement) conn.createstatement();
resultset rs=state.executequery(sql);
while(rs.next()){
//rs.getint("id");
int id=rs.getint(1);
string name=rs.getstring(2);
int sex=rs.getint(3);
//string time=rs.getstring("vtime");
system.out.println(id+"=="+name+"=="+sex+"==");
}
rs.close();
state.close();
conn.close();
} catch (classnotfoundexception e) {
// todo 自动生成的 catch 块
e.printstacktrace();
} catch (sqlexception e) {
// todo 自动生成的 catch 块
e.printstacktrace();
}
}
public static void main(string[] args) {
// todo 自动生成的方法存根
query();
}
}
希望本文所述对大家java程序设计有所帮助。
原文链接:https://blog.csdn.net/moliyiran/article/details/52419212

关注下面的标签,发现更多相似文章