package jdbc;
import java.sql.*;
public class Jdabc {
private static Connection Conn;
public static Connection getconnection( String url,String userName,String password) throws SQLException {
try {
// 注册驱动
Class.forName("com.mysql.jdbc.Driver");
System.out.println("加载驱动成功!!");
// 获得数据库对象
Conn = DriverManager.getConnection(url,userName,password);
System.out.println("连接数数据库对象");
} catch (Exception e) {
}
return Conn;
}
}
package jdbc;
import java.sql.*;
import java.util.Scanner;
public class SelectF extends Jdabc{
private Statement stat;
private ResultSet rs;
// 定义 数据库的链接方式 用户名和密码
private static String url="jdbc:mysql://localhost:3306/root";
private static String userName="root";
private static String password="root";
// 查询
public void select() {
try {
// 分别调用路径 账号和密码
java.sql.Connection conn = Jdabc.getconnection(url, userName, password);
// 定义sql 语句
String sql="select * from zhangwu";
// 获得SQL语句
stat = conn.createStatement();
// 执行SQL语句
rs = stat.executeQuery(sql);
while (rs.next()) {
// 收集数据库表中的结果集
String id=rs.getString(1);
String zname=rs.getString(2);
String mony=rs.getString(3);
String cla=rs.getString(4);
// 打印结果集
System.out.print(id+" ");
System.out.print(zname+" ");
System.out.print(mony+" ");
System.out.println(cla+" ");
}
// 释放资源
rs.close();
conn.close();
} catch (Exception e) {
}
}
// 添加
public void add() {
// 分别给数据库的属性赋值
String zname = null;
double mony=0 ;
String cla = null;
try {
// 分别调用路径 账号和密码
java.sql.Connection conn = Jdabc.getconnection(url,userName,password);
// 如果为空返回
if(conn==null)
return;
System.out.println("请输入你的信息!!");
Scanner sc=new Scanner(System.in);
System.out.println("请输入支出信息:");
zname = sc.nextLine();
System.out.println("请输入支出情况:");
mony = sc.nextDouble();
System.out.println("请输入每天或每月");
Scanner sc1=new Scanner(System.in);
cla = sc1.nextLine();
String sql="INSERT INTO zhangwu (zname,mony,cla)VALUES(?,?,?)";
// statement 对象调用sql语句
PreparedStatement ps = conn.prepareStatement(sql);
// 获得值
ps.setString(1, zname);
ps.setDouble(2, mony);
ps.setString(3, cla);
// 执行sql语句
int i= ps.executeUpdate();
System.out.println(i);
// 释放资源
ps.close();
conn.close();
} catch (Exception e) {
}
}
// 删除
public void Delete() {
// 同上解释
try {
java.sql.Connection conn = Jdabc.getconnection(url,userName,password);
System.out.println("请输入要删除的信息!!");
Scanner sc=new Scanner(System.in);
int id = sc.nextInt();
String sql="delete from zhangwu where id="+id;
stat = conn.createStatement();
int del = stat.executeUpdate(sql);
System.out.println(del);
stat.close();
conn.close();
} catch (SQLException e) {
}
}
// 更新查询
public void update() {
// 同上解释
int id;
try {
java.sql.Connection conn = Jdabc.getconnection(url,userName,password);
System.out.println("请输入要更改的信息!!");
Scanner sc=new Scanner(System.in);
id = sc.nextInt();
System.out.println("你更改的行");
int id1=sc.nextInt();
String sql=" UPDATE zhangwu SET id="+id+" WHERE id="+id1;
PreparedStatement pre = (PreparedStatement) conn.prepareStatement(sql);
int i = pre.executeUpdate();
System.out.println(i);
pre.close();
conn.close();
} catch (Exception e) {
}
}
}
package jdbc;
public class Insert {
public static void main(String[] args) {
SelectF sel=new SelectF();
// 分别调用正删改查
sel.select();
sel.add();
sel.Delete();
sel.update();
}
}