package com.transaction.demo.jdbc; import java.sql.*; import java.util.ArrayList; // JDBC class JDBC { /*public static void main(String[] args) throws SQLException { Connection connection = DriverManager.getConnection("jdbc:mysql://192.168.65.142:3306/test","root","root"); Statement statement = connection.createStatement(); String sql = "select * from user"; ResultSet resultSet = statement.executeQuery(sql); while(resultSet.next()){ System.out.println(resultSet.getObject("id")+" "+resultSet.getObject("name")+" "+resultSet.getObject("password")); } resultSet.close(); statement.close(); connection.close(); }*/ public static void main(String[] args) throws SQLException { Connection connection = DriverManager.getConnection("jdbc:mysql://192.168.65.142:3306/test","root","root"); connection.setAutoCommit(false); Statement statement = connection.createStatement(); int rows = statement.executeUpdate("insert into user(name,password) values('terry','terry')"); connection.commit(); System.out.println(rows); } } // 單例模式 class Singleton { private static Singleton instance = null; private Singleton() {} public static Singleton getInstance(){ if(instance == null){ instance = new Singleton(); } return instance; } public void sayHello(){ System.out.println("hello"); } } class Test{ public static void main(String[] args) { //單例模式 Singleton single = Singleton.getInstance(); single.sayHello(); //Singleton singleton = new Singleton(); 報錯 } } // 多線程 新建->就緒->運行->阻塞->死亡 class ThreadAndRunnable extends Thread implements Runnable{ @Override public void run() { System.out.println("線程4"); } public static void main(String[] args) { //lambda Thread t1 = new Thread(() -> System.out.println(Thread.currentThread().getName()+" 線程1")); t1.start(); //非lambda 匿名內部類 Thread t2 = new Thread(){ @Override public void run() { System.out.println(Thread.currentThread().getName()+" 線程2"); } }; t2.start(); Thread t3 = new Thread(new Runnable() { @Override public void run() { System.out.println(Thread.currentThread().getName()+" 線3"); } }); t3.start(); } //常規 /*public static void main(String[] args) { Thread t1 = new ThreadAndRunnable(); t1.start(); Runnable run = new ThreadAndRunnable(); Thread t2 = new Thread(run); t2.start(); }*/ } //棧 class Stack{ ArrayList<Object> list = new ArrayList<>(); //入棧 public void push(Object o){ list.add(o); } //出棧 public Object pop(){ Object o = list.get(list.size() - 1); list.remove(o); return o; } //棧是否爲空 public boolean isEmpty(){ return list.isEmpty(); } //棧大小 public int size(){ return list.size(); } //打印棧元素 @Override public String toString(){ return String.valueOf(list); } public static void main(String[] args) { //建立一個棧 Stack stack = new Stack(); //入棧 for(int i=1;i<=10;i++){ stack.push(i); } //出棧 while(!stack.isEmpty()){ System.out.println("棧:" + stack.toString() + "\t棧大小爲:" + stack.size() + "\t出棧元素爲:" + stack.pop()); } } } //隊列 class Queue{ ArrayList<Object> list = new ArrayList<>(); //入隊 public void in(Object o) { list.add(o); } //出隊 public Object out() { Object o = list.get(0); list.remove(o); return o; } //隊是否爲空 public boolean isEmpty() { return list.isEmpty(); } //隊大小 public int size() { return list.size(); } //打印隊元素 @Override public String toString() { return String.valueOf(list); } public static void main(String[] args) { //建立一個隊列 Queue queue = new Queue(); //入隊列 for(int i=1;i<=10;i++){ queue.in(i); } //出隊列 while(!queue.isEmpty()){ System.out.println("隊:" + queue.toString() + "\t隊大小爲:" + queue.size() + "\t出隊元素爲:" + queue.out()); } } } // B樹 class BTree{ public int data; public BTree left; public BTree rigth; public boolean hasLeft(){ return left != null; } public boolean hasRigth(){ return rigth != null; } public BTree(){} public static void main(String[] args) { BTree root = new BTree(); root.data = 0; BTree node1 = new BTree(); node1.data = 1; BTree node2 = new BTree(); node2.data = 2; BTree node3 = new BTree(); node3.data = 3; BTree node4 = new BTree(); node4.data = 4; BTree node5 = new BTree(); node5.data = 5; BTree node6 = new BTree(); node6.data = 6; root.left = node1; root.rigth = node2; node1.left = node3; node1.rigth = node4; node2.left = node5; node2.rigth = node6; System.out.println("先序遍歷二叉樹:"); queryFirst(root); System.out.println(); System.out.println("中序遍歷二叉樹:"); queryMiddle(root); System.out.println(); System.out.println("後序遍歷二叉樹:"); queryLast(root); System.out.println(); } //先序遍歷二叉樹 public static void queryFirst(BTree tree){ if(tree == null){ return; } System.out.print(tree.data+"\t"); if(tree.hasLeft()){ queryFirst(tree.left); } if(tree.hasRigth()){ queryFirst(tree.rigth); } } //中序遍歷二叉樹 public static void queryMiddle(BTree tree){ if(tree == null){ return; } if(tree.hasLeft()){ queryMiddle(tree.left); } System.out.print(tree.data+"\t"); if(tree.hasRigth()){ queryMiddle(tree.rigth); } } //後序便利二叉樹 public static void queryLast(BTree tree){ if(tree == null){ return; } if(tree.hasLeft()){ queryLast(tree.left); } if(tree.hasRigth()){ queryLast(tree.rigth); } System.out.print(tree.data+"\t"); } } // 冒泡 class BubbleSort{ public static void sort(int a[]){ int temp = 0; for(int i = 0;i < a.length;i++){ for(int j = i;j < a.length;j++){ if(a[i] > a[j]){ temp = a[i]; a[i] = a[j]; a[j] = temp; } } } } public static void main(String[] args) { int[] a = {1,3,5,2,4,8,6,7,10,9}; sort(a); for(int i = 0;i < a.length;i++){ System.out.println(a[i]); } } }