Java對mysql資料庫的增刪改查創表5個操作
阿新 • • 發佈:2019-01-23
package mywork.com; import java.sql.*; import java.text.DecimalFormat; // DecimalFormat df = new DecimalFormat("#0.000"); import java.util.Scanner; import java.math.*; import java.io.*; import java.text.*; import java.util.Arrays; // Arrays.sort(s); import java.math.BigInteger; import java.util.Queue; import java.util.LinkedList; import java.util.Random; public class code1 { public final static Scanner cin = new Scanner(System.in); public static void main(String[] args) { int a = 6; // // System.out.println(a); // try { // Class.forName("com.mysql.jdbc.Driver"); // 線載入驅動 // // System.out.println(888); // } catch (ClassNotFoundException e) { // System.out.println("找不到驅動程式類 ,載入驅動失敗!"); // e.printStackTrace(); // } try { Class.forName("com.mysql.jdbc.Driver"); // 先載入驅動 // System.out.println(888); String url = "jdbc:mysql://localhost:3306/test"; // 資料庫名稱 String username = "root"; // 賬號 String password = "root"; // 密碼 Connection con = DriverManager.getConnection(url, username, password); // 通過這個介面連線資料庫。 // String sql = // "CREATE TABLE tableName5(id int not null, name varchar(20) not null, age int null, primary key (id));"; // // 建立一個表,名字為tablename (id為int,name為字串,age為int,這個好像是主碼?主碼為id) // PreparedStatement pstmt = con.prepareStatement(sql); // pstmt.executeUpdate(); String[] createtable = new String[100]; for (int i = 0; i < 0; i++) { cin.useDelimiter("\n"); // 輸入帶有空格的字串 createtable[i] = cin.nextLine(); // String sql = // "CREATE TABLE tableName5(id int not null, name varchar(20) not null, age int null, primary key (id));"; // // 建立一個表,名字為tablename (id為int,name為字串,age為int,這個好像是主碼?主碼為id) // 這樣就可以直接通過輸入建立多個表了,很簡單,,只要改改名字就可以拉? PreparedStatement pstmt = con.prepareStatement(createtable[i]); pstmt.executeUpdate(); } String[] insert = new String[100]; for (int i = 0; i < 0; i++) { // String sqlq=cin.next(); cin.useDelimiter("\n"); // 輸入帶有空格的字串 insert[i] = cin.nextLine(); // String sqlq // ="insert into tableName(id,name,age) values(524323,'123456',22)"; // 這個直接改成輸入,,然後就可以插入N條資料啦= = // 插入時注意每個主碼必須是不同的。 Statement stmt = con.createStatement(); stmt.executeUpdate(insert[i]); } String[] delete = new String[100]; for (int i = 0; i < 0; i++) { cin.useDelimiter("\n"); // 輸入帶有空格的字串 delete[i] = cin.nextLine(); // String qq="delete from tablename5 where id>=2;"; // 直接輸入要刪除的表的特徵,然後就可以刪除N條資料啦。 Statement stmt1 = con.createStatement(); stmt1.executeUpdate(delete[i]); } String[] update = new String[100]; for (int i = 0; i < 0; i++) { cin.useDelimiter("\n"); // 輸入帶有空格的字串 update[i] = cin.nextLine(); // String // sql11="update tablename set age=20,set name='lalal' where id=52323 ";//生成一條mysql語句 // 直接輸入要更新的表的特徵,然後就可以更新N條資料啦。 Statement lalala = con.createStatement(); lalala.executeUpdate(update[i]); } String[] query = new String[100]; for (int i = 0; i < 0; i++) { // String sql123 = // "SELECT `id`, `name`,`age` FROM `test`.`tablename` LIMIT 0, 1000;"; // 要執行的SQL cin.useDelimiter("\n"); // 輸入帶有空格的字串 query[i] = cin.nextLine(); // 還是一個套路啊,將上面的sql語句改成輸入,,然後tablename換一下名字,0,1000為1000條表中的資料 Statement stmt11 = con.createStatement(); ResultSet rs = stmt11.executeQuery(query[i]);// 建立資料物件,通過rs來呼叫表中的資料 System.out.println("編號" + "\t" + "姓名" + "\t" + "年齡"); while (rs.next()) { System.out.print(rs.getInt(1) + "\t"); // 第一個資料 System.out.print(rs.getString(2) + "\t"); // 第二個為字串型別的 System.out.print(rs.getInt(3) + "\t"); // 第三個 System.out.println(); } rs.close(); stmt11.close(); con.close(); } System.out.println("success"); con.close(); } catch (ClassNotFoundException e) { System.out.println("找不到驅動程式類 ,載入驅動失敗!"); e.printStackTrace(); } catch (SQLException se) { System.out.println("失敗"); se.printStackTrace(); } } }