发布网友 发布时间:2022-04-24 06:32
共2个回答
懂视网 时间:2022-05-01 01:13
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) + " " + rs.getString(2) + " " + rs.getString(3) + " " + rs.getString(4) + " "); // 方式2 /* * System.out.println(rs.getString("pk_Test_ID") + " " + rs.getString("name") + * " " + rs.getString("subject") + " " + rs.getString("result") + " "); */ } } 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:tufujiepuyang@foxmail.com * @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") + " " + result.getString("name") + " " + result.getString("subject") + " " + result.getString("result")); } // 指定第几笔数据 System.out.println("指定第几笔数据,这里指定第4笔数据"); result.absolute(4); System.out.println(result.getString("pk_Test_ID") + " " + result.getString("name") + " " + result.getString("subject") + " " + result.getString("result")); // 从目前游标处指定游标位置 System.out.println("从目前游标处指定游标位置,这里向下移动2笔数据"); result.relative(2); System.out.println(result.getString("pk_Test_ID") + " " + result.getString("name") + " " + result.getString("subject") + " " + result.getString("result")); System.out .println("同样,first(),last(),beforeFirst()效果类似,first()移动到第一笔数据,last()移动到最后一笔数据"); result.first(); System.out.println(result.getString("pk_Test_ID") + " " + result.getString("name") + " " + result.getString("subject") + " " + result.getString("result")); result.last(); System.out.println(result.getString("pk_Test_ID") + " " + result.getString("name") + " " + result.getString("subject") + " " + result.getString("result")); result.close(); stmt.close(); conn.close(); } catch (Exception e) { e.printStackTrace(); } } }
Java数据库增删改查
标签:
热心网友 时间:2022-04-30 22:21
你所说的增删改查是数据库的命令操作。在Java编程中,在Java和数据库完成连接以后,可以调用数据库的select、delete、updata等命令。你也可以把相关的命令变成一个字符串对象,这样调用起来会更简单。