数据库为MySQL数据库,Oracle数据库类似:
create database db_test;--创建数据库 create user user_test@localhost identified by ‘123456‘;--创建用户 grant all privileges on db_test.* to user_test@localhost;--给用户授予权限 use db_test; create table tb_Test( pk_Test_ID char(10) primary key, name varchar(30), subject varchar(30), result numeric(3) ); insert into tb_Test values(1,‘tufujie‘,‘语文‘,‘100‘); insert into tb_Test values(2,‘dage‘,‘语文‘,‘100‘); insert into tb_Test values(3,‘ranye‘,‘语文‘,‘100‘); insert into tb_Test values(4,‘haonan‘,‘语文‘,‘100‘);
插入数据
package com.tfj.db; import java.sql.Connection; import java.sql.DriverManager; //演示如何使用com.mysql.jdbc连接桥连接Mysql,插入内容,共三种方式:静态的两种executeUpdate(),executeBatch(),动态的PrepareStatement,实时动态时可以采用PrepareStatement()和executeBatch()的结合,删除、修改都有这三种方法 public class JavaCtMysqlInsert { public static void main(String[] args) { try { // 1.加载(载入)驱动 Class.forName("com.mysql.jdbc.Driver"); // 2.得到链接(注册) Connection ct = DriverManager.getConnection("jdbc:mysql://localhost:3306/db_test", "user_test", "123456"); if (ct != null && !ct.isClosed()) System.out.println("数据库测试连接成功"); ct.setAutoCommit(false); java.sql.PreparedStatement ps = ct .prepareStatement("insert into tb_Test values(?, ?, ?, ?)"); ps.setString(1, "5"); ps.setString(2, "ins0"); ps.setString(3, "语文"); ps.setString(4, "100"); ps.executeUpdate(); ps.clearParameters(); ct.commit(); ct.close(); } catch (Exception e) { e.printStackTrace(); } } }
package com.tfj.db; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Statement; //演示如何使用com.mysql.jdbc连接桥连接Mysql,查询表中内容 public class JavaCtMysqlInsert2 { public static void main(String[] args) { try { Class.forName("com.mysql.jdbc.Driver"); Connection ct = DriverManager.getConnection("jdbc:mysql://localhost:3306/db_test", "user_test", "123456"); if (ct != null && !ct.isClosed()) System.out.println("数据库测试连接成功"); ct.setAutoCommit(false); Statement sm = ct.createStatement(); sm.addBatch("insert into tb_Test values(‘6‘,‘ins1‘, ‘语文‘, 100)"); sm.addBatch("insert into tb_Test values(‘7‘,‘ins2‘, ‘语文‘, 100)"); sm.addBatch("insert into tb_Test values(‘8‘,‘ins3‘, ‘语文‘, 100)"); sm.addBatch("insert into tb_Test values(‘9‘,‘ins4‘, ‘语文‘, 100)"); sm.executeBatch(); ct.commit(); sm.close(); ct.close(); } catch (Exception e) { e.printStackTrace(); } } }
package com.tfj.db; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Statement; //演示如何使用com.mysql.jdbc连接桥连接MySQL,并插入内容 public class JavaCtMysqlInsert3 { public static void main(String[] args) { try { Class.forName("com.mysql.jdbc.Driver"); Connection ct = DriverManager.getConnection("jdbc:mysql://localhost:3306/db_test", "user_test", "123456"); if (ct != null && !ct.isClosed()) System.out.println("数据库测试连接成功"); ct.setAutoCommit(false); Statement sm = ct.createStatement(); sm.executeUpdate("insert into tb_Test values(‘10‘,‘ins5‘, ‘语文‘, ‘100‘)"); ct.commit(); sm.close(); ct.close(); } catch (Exception e) { e.printStackTrace(); } } }
删除数据
package com.tfj.db; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Statement; //演示如何使用com.mysql.jdbc连接桥连接MySQL,并删除内容 public class JavaCtMysqlDelete { public static void main(String[] args) { try { Class.forName("com.mysql.jdbc.Driver"); Connection ct = DriverManager.getConnection("jdbc:mysql://localhost:3306/db_test", "user_test", "123456"); if (ct != null && !ct.isClosed()) System.out.println("数据库测试连接成功"); Statement sm = ct.createStatement(); sm.executeUpdate("delete from tb_Test where name=‘tufujie‘"); sm.close(); ct.close(); } catch (Exception e) { e.printStackTrace(); } } }
package com.tfj.db; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; //演示如何使用com.mysql.jdbc连接桥连接MySQL,并删除内容 public class JavaCtMysqlDelete2 { public static void main(String[] args) { try { Class.forName("com.mysql.jdbc.Driver"); Connection ct = DriverManager.getConnection("jdbc:mysql://localhost:3306/db_test", "user_test", "123456"); if (ct != null && !ct.isClosed()) System.out.println("数据库测试连接成功"); Statement sm = ct.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); ResultSet rs = sm.executeQuery("select * from tb_Test where name=‘dage‘");// 从查询到的内容中进行修改、插入和删除 rs.last(); rs.deleteRow(); rs.close(); sm.close(); ct.close(); } catch (Exception e) { e.printStackTrace(); } } }
修改数据
package com.tfj.db; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Statement; //演示如何使用com.mysql.jdbc连接桥连接MySQL,并修改内容 public class JavaCtMysqlUpdate { public static void main(String[] args) { try { Class.forName("com.mysql.jdbc.Driver"); Connection ct = DriverManager.getConnection("jdbc:mysql://localhost:3306/db_test", "user_test", "123456"); if (ct != null && !ct.isClosed()) System.out.println("数据库测试连接成功"); Statement sm = ct.createStatement(); sm.executeUpdate("update tb_Test set name=‘ran‘ where name=‘ranye‘"); sm.close(); ct.close(); } catch (Exception e) { e.printStackTrace(); } } }
查询数据
package com.tfj.db; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; //演示如何使用com.mysql.jdbc连接桥连接MySQL,查询表中内容 public class JavaCtMysqlSelect { private static int rs2; public static void main(String[] args) { try { Class.forName("com.mysql.jdbc.Driver"); Connection ct = DriverManager.getConnection("jdbc:mysql://localhost:3306/db_test", "user_test", "123456"); if (ct != null && !ct.isClosed()) System.out.println("数据库测试连接成功"); Statement sm = ct.createStatement(); ResultSet rs = null; boolean flag = sm.execute("select * from tb_Test"); if (flag) {// 说明是查询 rs = sm.executeQuery("select * from tb_Test"); while (rs.next()) { System.out.println(rs.getString(1) + "\t" + rs.getString(2) + "\t" + rs.getString(3) + "\t" + rs.getString(4) + "\t"); // 方式2 /* * System.out.println(rs.getString("pk_Test_ID") + "\t" + rs.getString("name") + * "\t" + rs.getString("subject") + "\t" + rs.getString("result") + "\t"); */ } } else rs2 = sm.getUpdateCount(); // 关闭打开的资源 rs.close(); sm.close(); ct.close(); } catch (Exception e) { e.printStackTrace(); } } }
package com.tfj.db; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; /** * @author Jacksile E-mail:[email protected] * @date 2015年9月5日 上午11:33:56 * @function 如何使用ResultSet控制游标 */ public class JavaCtMysqlSelect2 { public static void main(String[] args) { try { Class.forName("com.mysql.jdbc.Driver"); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db_test", "user_test", "123456"); if (conn != null && !conn.isClosed()) { System.out.println("数据库连接成功"); } Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); ResultSet result = stmt.executeQuery("select * from tb_Test"); result.afterLast(); while (result.previous()) { System.out.println(result.getString("pk_Test_ID") + "\t" + result.getString("name") + "\t" + result.getString("subject") + "\t" + result.getString("result")); } // 指定第几笔数据 System.out.println("指定第几笔数据,这里指定第4笔数据"); result.absolute(4); System.out.println(result.getString("pk_Test_ID") + "\t" + result.getString("name") + "\t" + result.getString("subject") + "\t" + result.getString("result")); // 从目前游标处指定游标位置 System.out.println("从目前游标处指定游标位置,这里向下移动2笔数据"); result.relative(2); System.out.println(result.getString("pk_Test_ID") + "\t" + result.getString("name") + "\t" + result.getString("subject") + "\t" + result.getString("result")); System.out .println("同样,first(),last(),beforeFirst()效果类似,first()移动到第一笔数据,last()移动到最后一笔数据"); result.first(); System.out.println(result.getString("pk_Test_ID") + "\t" + result.getString("name") + "\t" + result.getString("subject") + "\t" + result.getString("result")); result.last(); System.out.println(result.getString("pk_Test_ID") + "\t" + result.getString("name") + "\t" + result.getString("subject") + "\t" + result.getString("result")); result.close(); stmt.close(); conn.close(); } catch (Exception e) { e.printStackTrace(); } } }
时间: 2024-10-08 06:25:08