数据库基本操作
查询 select * from tb_1
插入 insert into tb_1(name,age,sex) value( )
更新 update tb_1 set name=" " where
删除 delete from tb_1 where
java中操作数据库
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.Statement;
public class test_1 {
static final String url="jdbc:mysql://localhost:1111/hbu"; //jdbc:msql://localhost:端口名字/连接的数据库名字
static final String username="root"; //数据库用户名
static final String password="123456"; //数据库密码
//1 查找
public static void main (String[] args) throws ClassNotFoundException, SQLException {
String sql="delete from tb_1 where id=1";
delete(sql);}
public static void search(String sql) throws ClassNotFoundException, SQLException {
//加载驱动 报错的原因:这个类可能找不到 解决方法:抛出或者try/catch
Class.forName("com.mysql.cj.jdbc.Driver");
//用户信息和url
//驱动管理类调用方法进行连接 创建连接对象connection connection代表数据库
Connection connection=(Connection) DriverManager.getConnection(url, username, password);
//创建sql执行对象
Statement statement = (Statement) connection.createStatement();
//resultset处理数据
ResultSet resultset= statement.executeQuery(sql);
while(resultset.next()) {
String id=resultset.getString("id");
String name=resultset.getString("name");
System.out.println(id+" "+name);
}
//释放资源
if(resultset!=null) {
resultset.close();
}
if(statement!=null) {
statement.close();
}
if(connection!=null) {
connection.close();
}
}
//2 更新
public static void update(String sql) throws ClassNotFoundException, SQLException {
//加载驱动 报错的原因:这个类可能找不到 解决方法:抛出或者try/catch
Class.forName("com.mysql.cj.jdbc.Driver"); //数据库版本不同驱动方法不同 这是8. 版本的
// Class.forName("com.mysql.jdbc.Driver"); //这是5.版本的
//用户信息和url
//驱动管理类调用方法进行连接 创建连接对象connection connection代表数据库
Connection connection=(Connection) DriverManager.getConnection(url, username, password);
//创建sql执行对象
Statement statement = (Statement) connection.createStatement();
int num=statement.executeUpdate(sql);
if(num>0) {
System.out.println("修改成功");
}else {
System.out.println("修改失败");
}
//释放资源
if(statement!=null) {
statement.close();
}
if(connection!=null) {
connection.close();
}
}
//3添加
public static void insert(String sql) throws ClassNotFoundException, SQLException {
//加载驱动 报错的原因:这个类可能找不到 解决方法:抛出或者try/catch
Class.forName("com.mysql.cj.jdbc.Driver");
//用户信息和url
//驱动管理类调用方法进行连接 创建连接对象connection connection代表数据库
Connection connection=(Connection) DriverManager.getConnection(url, username, password);
//创建sql执行对象
Statement statement = (Statement) connection.createStatement();
int num=statement.executeUpdate(sql);
if(num>0) {
System.out.println("添加成功");
}else {
System.out.println("添加失败");
}
//释放资源
if(statement!=null) {
statement.close();
}
if(connection!=null) {
connection.close();
}
}
//4 删除
public static void delete(String sql) throws ClassNotFoundException, SQLException {
//加载驱动 报错的原因:这个类可能找不到 解决方法:抛出或者try/catch
Class.forName("com.mysql.cj.jdbc.Driver");
//用户信息和url
//驱动管理类调用方法进行连接 创建连接对象connection connection代表数据库
Connection connection=(Connection) DriverManager.getConnection(url, username, password);
//创建sql执行对象
Statement statement = (Statement) connection.createStatement();
int num=statement.executeUpdate(sql);
if(num>0) {
System.out.println("删除成功");
}else {
System.out.println("删除失败");
}
//释放资源
if(statement!=null) {
statement.close();
}
if(connection!=null) {
connection.close();
}
}