範例:JDBC查詢 package com.hsp; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import jav ...
範例:JDBC查詢
package com.hsp;
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import java.util.Date;
public class testDatabase {
/** * @param args */ public static void main(String[] args) { Connection con = null; String DBname = "jdb"; //資料庫名字 String url = "jdbc:mysql://localhost:3306/"+DBname+"?useSSL=true&characterEncoding=utf-8"; String username = "root";//資料庫賬號 String password = "root";//資料庫密碼 Statement sql = null; ResultSet rs = null; try { Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection(url, username, password);//連接代碼 sql = con.createStatement(); rs = sql.executeQuery("SELECT * FROM students");//查詢student表中的數據 while(rs.next()){ String number = rs.getString(1); String name = rs.getString(2); Date date = rs.getDate(3); float height = rs.getFloat(4); System.out.println("number:"+number+" name:"+name+" date:"+date+" height:"+height); } con.close();//con必須等rs調用完數據後才能關閉,否則查詢不到數據 } catch (Exception e) { // TODO: handle exception System.out.println(e); } } } |
範例:封裝JDBC查詢
package com.hsp;
import java.sql.*;
public class JdbcMysql {
public static void main(String[] args) throws SQLException, ClassNotFoundException { // TODO Auto-generated method stub Connection con = null; Statement sql = null; ResultSet rs = null; con = GetDBConnection.ConnectDB("jdb", "root", "root"); String query = "SELECT * FROM STUDENTS"; if(con==null){ System.out.println("連接錯誤"); return ; } try { sql = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); rs = sql.executeQuery(query); rs.last(); int max = rs.getRow(); System.out.println("表共有"+max+"條記錄"+",隨機抽取2條記錄"); int a[] = GetRandomNmber.getRandomNumber(max,2); for(int i:a){ rs.absolute(i); String number = rs.getString(1); String name = rs.getString(2); Date date = rs.getDate(3); float height = rs.getFloat(4); System.out.println("number:"+number+" name:"+name+" date:"+date+" height:"+height); } con.close(); } catch (Exception e) { // TODO: handle exception System.out.println(e); } }
} |
package com.hsp;
import java.sql.Connection; import java.sql.DriverManager;
public class GetDBConnection { public static Connection ConnectDB(String DBname,String username,String password) { Connection con = null;
String url = "jdbc:mysql://localhost:3306/"+DBname+"?useSSL=true&characterEncoding=utf-8"; try { Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection(url, username, password); } catch (Exception e) { // TODO: handle exception System.out.println(e); } return con; } } |
package com.hsp;
import java.util.Random;
public class GetRandomNmber { public static int[] getRandomNumber(int max,int amount){ //1至max之間的amount個不同隨機整數 包括(1和max) int [] randomNumber = new int[amount]; int index = 0; randomNumber[0] = -1; Random random = new Random(); while(index<amount){ int number = random.nextInt(max)+1; boolean isInArrays = false; for(int m:randomNumber){//m依次去數組randomNumber元素的值 if(m==number) isInArrays = true;//number在數組裡了 } if(isInArrays==false){ //如果number不在數組randomNumber中 randomNumber[index] = number; index++; } } return randomNumber; } } |
範例:JDBC多條件查詢
package com.hsp;
import java.sql.*;
public class JdbcMysql {
public static void main(String[] args) throws SQLException, ClassNotFoundException { // TODO Auto-generated method stub Connection con = null; Statement sql = null; ResultSet rs = null; con = GetDBConnection.ConnectDB("jdb", "root", "root"); String query = "SELECT * FROM STUDENTS"; String c1 = " year(birthday)<=2000 and month(birthday)>7";//查詢條件1 String c2 = " name Like 'z_%'";//查詢條件2 String c3 = " height >1.65";//查詢條件3 String sqlStr = "SELECT * FROM students where "+c1+" and "+c2+" and "+c3+" order by birthday"; if(con==null){ System.out.println("連接錯誤"); return ; } try { sql = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); rs = sql.executeQuery(sqlStr); while(rs.next()){ String number = rs.getString(1); String name = rs.getString(2); Date date = rs.getDate(3); float height = rs.getFloat(4); System.out.println("number:"+number+" name:"+name+" date:"+date+" height:"+height); } con.close(); } catch (Exception e) { // TODO: handle exception System.out.println(e); } }
} |
package com.hsp;
import java.sql.Connection; import java.sql.DriverManager;
public class GetDBConnection { public static Connection ConnectDB(String DBname,String username,String password) { Connection con = null;
String url = "jdbc:mysql://localhost:3306/"+DBname+"?useSSL=true&characterEncoding=utf-8"; try { Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection(url, username, password); } catch (Exception e) { // TODO: handle exception System.out.println(e); } return con; } } |
範例:JDBC插入數據
package com.hsp;
import java.sql.*;
public class JdbcMysql {
public static void main(String[] args) throws SQLException, ClassNotFoundException { // TODO Auto-generated method stub Connection con = null; Statement sql = null; ResultSet rs = null; con = GetDBConnection.ConnectDB("jdb", "root", "root"); String query = "SELECT * FROM STUDENTS"; String jilu = "('R1011','JS','2000-10-23',1.66),"+ "('R1010','LW','1989-7-22',1.76)"; String sqlStr = "insert into students values"+jilu; if(con==null){ System.out.println("連接錯誤"); return ; } try { sql = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); int ok = sql.executeUpdate(sqlStr); rs = sql.executeQuery("select * from students"); while(rs.next()){ String number = rs.getString(1); String name = rs.getString(2); Date date = rs.getDate(3); float height = rs.getFloat(4); System.out.println("number:"+number+" name:"+name+" date:"+date+" height:"+height); } con.close(); } catch (Exception e) { // TODO: handle exception System.out.println(e); } }
} |
package com.hsp;
import java.sql.Connection; import java.sql.DriverManager;
public class GetDBConnection { public static Connection ConnectDB(String DBname,String username,String password) { Connection con = null;
String url = "jdbc:mysql://localhost:3306/"+DBname+"?useSSL=true&characterEncoding=utf-8"; try { Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection(url, username, password); } catch (Exception e) { // TODO: handle exception System.out.println(e); } return con; } } |