package com;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.mysql.jdbc.Driver;
public class Demo_Jdbc {
public static void main(String[] args) throws SQLException {
//导包
//注册驱动
DriverManager.registerDriver(new Driver()); //告诉java程序我要用哪个驱动,实际上是告诉java虚拟机
//获取连接 连接方式 连接的数据库 本机地址 端口 数据库
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/gjp", "root", "12");
//获取statement接口的
Statement stat = conn.createStatement();
//发送sql语句,获取结果集
String sql2 = "UPDATE gjp_ledger SET ldesc = '捡钱了' WHERE ID = 8";
String sql = "SELECT * FROM gjp_ledger";
int i = stat.executeUpdate(sql2);
if(i > 0) {
System.out.println("修改成功!");
}
ResultSet result = stat.executeQuery(sql);
//操作结果集
while(result.next()) {
int ID = result.getInt("ID");
String ldesc = result.getString("ldesc");
Date createtime = result.getDate("createtime");
double money = result.getDouble("money");
System.out.println(ID + " " + ldesc + " " + createtime + " " + money);
}
//释放资源
result.close();
stat.close();
conn.close();
}
}
|
|