JDBC的安裝html
首先在登陸MySQL的官網下載JDBC-MySQL數據庫驅動,或者去www.mysql.com/products/connector直接下載。java
由於jdbc包屬於第三方包,所以要本身導入,下面是導入的方法:mysql
https://jingyan.baidu.com/article/3aed632e1a4ceb70108091f6.htmlsql
導入以後就建立一個connect類來編寫代碼,來測試是否能與服務器鏈接。數據庫
import java.sql.*;//導入sql包 public class connect { public static void main(String args[]) throws SQLException, ClassNotFoundException { //數據庫鏈接 Statement sql;//數據庫 ResultSet rs;//數據 Connection conn;//用於鏈接 String url = "jdbc:mysql://localhost:3306/students?serverTimezone=UTC&useSSL=false"; String username = "root"; String password = "123456";//密碼本身修改 //Class.forName("com.mysql.cj.jdbc.Driver");//加載JDBC-MySQL驅動 conn = DriverManager.getConnection(url,username,password);//鏈接完畢 try{ Class.forName("com.mysql.cj.jdbc.Driver");//加載JDBC-MySQL驅動 conn = DriverManager.getConnection(url,username,password); if(conn != null){ System.out.println("數據庫鏈接成功!"); }else{ System.out.println("數據庫鏈接失敗!"); } }catch(ClassNotFoundException e){ e.printStackTrace(); }catch(SQLException e){ e.printStackTrace(); } }
}
若是數據庫能夠鏈接以後就能夠來試一下數據庫的基本操做;服務器
import java.sql.*;//導入sql包 public class connect { public static void main(String args[]) throws SQLException, ClassNotFoundException { //數據庫鏈接 Statement sql;//數據庫 ResultSet rs;//數據 Connection conn;//用於鏈接 String url = "jdbc:mysql://localhost:3306/students?serverTimezone=UTC&useSSL=false"; String username = "root"; String password = "123456";//密碼 //Class.forName("com.mysql.cj.jdbc.Driver");//加載JDBC-MySQL驅動 conn = DriverManager.getConnection(url,username,password);//鏈接完畢 //添加篩選條件 String c1 = " year(birthday)<=2000 and month(birthday)>7"; String c2 = " name Like '張_%' "; String c3 = " height >1.65"; String sqlStr="select * from mess where" +c1+ " and "+c2+ " and "+c3+"order by birthday"; try { sql = conn.createStatement(); 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.printf("%s\t",number); System.out.printf("%s\t",name); System.out.printf("%s\t",date); System.out.printf("%.2f\t",height); System.out.printf("\n"); } //conn.close(); } catch(SQLException e) { System.out.println(e); } System.out.println("--------華麗的分割線---------"); /*try{ Class.forName("com.mysql.cj.jdbc.Driver");//加載JDBC-MySQL驅動 conn = DriverManager.getConnection(url,username,password); if(conn != null){ System.out.println("數據庫鏈接成功!"); }else{ System.out.println("數據庫鏈接失敗!"); } }catch(ClassNotFoundException e){ e.printStackTrace(); }catch(SQLException e){ e.printStackTrace(); }*/ //順序查詢 try { //conn = DriverManager.getConnection(url,username,password); sql = conn.createStatement(); rs = sql.executeQuery("SELECT*FROM mess"); 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.printf("%s\t",number); System.out.printf("%s\t",name); System.out.printf("%s\t",date); System.out.printf("%.2f\t",height); System.out.printf("\n"); } conn.close(); } catch(SQLException e) { System.out.println(e); } } }
https://blog.csdn.net/weixin_42323802/article/details/82589743測試