JDBC的安裝
首先在登錄MySQL的官網下載JDBC-MySQL數據庫驅動,或者去www.mysql.com/products/connector直接下載。
因為jdbc包屬於第三方包,因此要自己導入,下面是導入的方法:
https://jingyan.baidu.com/article/3aed632e1a4ceb70108091f6.html
導入之后就創建一個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); } } }
可能會遇到的問題:
JDBC連接MYSQL數據庫失敗,Loading class `com.mysql.jdbc.Driver'. This is deprecated.
https://blog.csdn.net/weixin_42323802/article/details/82589743
