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测试