Java 程序练习 JDBC的增删改查
import java.sql.*; import java.util.Scanner; public class test00 { public static void main(String[] args) { int a; Scanner sc=new Scanner(System.in); while(true) { System.out.println("请输入操作:"); System.out.println("1.增"); System.out.println("2.删"); System.out.println("3.改"); System.out.println("4.查"); System.out.println("5.查询所有"); a=sc.nextInt(); if(a>0&&a<6) { switch(a) { case 1:add();break; case 2:delete();break; case 3:update();break; case 4:dectet();break; case 5:all();break; } } else System.out.println("----输入错误---"); } } private static void all() { PreparedStatement ps=null; ResultSet rs=null; String sql="select id,name,loc from test"; ps=Util(sql); try { rs=ps.executeQuery(sql); while(rs.next()) { System.out.print(rs.getString("id")+"--"); System.out.print(rs.getString("name")+"--"); System.out.println(rs.getString("loc")); } } catch (SQLException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } try { rs.close(); } catch (SQLException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } try { ps.close(); } catch (SQLException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } private static PreparedStatement Util(String sql) { Connection conn=null; PreparedStatement pt=null; ResultSet rs=null; try { Class.forName("com.mysql.jdbc.Driver"); conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/JDBCTest","root","zber1574"); pt=conn.prepareStatement(sql); } catch (ClassNotFoundException | SQLException e) { e.printStackTrace(); } return pt; } private static void dectet() { Scanner sc=new Scanner(System.in); PreparedStatement pr=null; Connection conn=null; ResultSet rs=null; String id,name,loc; System.out.println("请输入要c查询的id"); id=sc.next(); String sql="select id,name,loc from test"; try { Class.forName("com.mysql.jdbc.Driver"); conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/JDBCTest","root","zber1574"); pr=conn.prepareStatement(sql); rs=pr.executeQuery(sql); while(rs.next()){ if(id.equals(rs.getString("id"))) { System.out.print(rs.getString("id")+"--"); System.out.print(rs.getString("name")+"--"); System.out.println(rs.getString("loc")); } } } catch (ClassNotFoundException | SQLException e) { e.printStackTrace(); } } private static void update() { PreparedStatement pr=null; Scanner sc=new Scanner(System.in); String id,name,loc; System.out.println("请输入要更改的id"); String U_id=sc.next(); System.out.println("请输入要更改的name ,loc"); name=sc.next(); loc=sc.next(); String sql="update test set id='"+U_id+"',name='"+name+"',loc='"+loc+"'where id='"+U_id+"'"; System.out.println(sql); pr=Util(sql); try { int count=pr.executeUpdate(); System.out.println(count==1?"更改成功":"更改失败"); } catch (SQLException e) { e.printStackTrace(); } } private static void delete() { PreparedStatement pr=null; Scanner sc=new Scanner(System.in); String id,name,loc; System.out.println("请输入要删除的id"); id=sc.next(); String sql="delete from test where id ="+id; pr=Util(sql); try { int count=pr.executeUpdate(); System.out.println(count==1?"删除成功":"删除失败"); } catch (SQLException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } private static void add() { Scanner sc=new Scanner(System.in); String id,name,loc; System.out.println("输入id,name,loc"); id=sc.next(); name=sc.next(); loc=sc.next(); PreparedStatement pr=null; String sql= null; System.out.println(); sql="insert into test (id,name,loc) values(?,?,?)"; pr=Util(sql); try { pr.setString(1, id); pr.setString(2, name); pr.setString(3, loc); int count=pr.executeUpdate(); System.out.println(count==1?"添加成功":"添加失败"); } catch (SQLException e) { e.printStackTrace(); } } }
自己尝试做的Java程序连接数据库完成增删改查,
连接数据库步骤分别是加载驱动程序:
Class.forName(driverClass)
//加载MySql驱动
Class.forName("com.mysql.jdbc.Driver")
//加载Oracle驱动
Class.forName("oracle.jdbc.driver.OracleDriver")
获得数据库连接:
DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/imooc", "root", "root");
创建Statement\PreparedStatement对象:
conn.createStatement();
conn.prepareStatement(sql);