java连接mysql5.1 教程(含代码)+ 查询数据
附:
连接数据库需要用到以下文件:
mysql-connector-java-5.1.41-bin.jar;
文件链接见教程
创建java项目(略)
参考图片:
导入链接文件
创建libs文件夹
右键Example文件,选择 new –> folder ,输入 libs ,点击finish。
将连接文件导入libs文件夹
复制mysql-connector-java-5.1.41-bin.jar文件,拷贝到libs文件夹下。
构建路径MySQL驱动程序jar包
在工具栏找到 Project –> Properties,点击后找到Java Build Path
选择上方的Libraries,点击右侧的Add JARs…,选择刚刚添加入项目libs中的MySQL驱动程序jar包,然后点击Apply and Close。
测试连接MySQl数据库(代码)
在example1.class文件里写入代码,代码如下:
package t1; import java.sql.*; public class example1 { //MySQL 8.0 以下版本 - JDBC 驱动名及数据库 URL //数据库:message Host Address:localhost 端口:3306 //请根据实际数据库的信息进行修改 static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; static final String DB_URL = "jdbc:mysql://localhost:3306/message"; // 数据库的用户名与密码 //用户名:root 密码:123456 //请根据实际数据库的信息进行修改 static final String USER = "root"; static final String PASS = "123456"; public static void main(String[] args) { // TODO Auto-generated method stub Connection conn = null; Statement stmt = null; try{ // 注册 JDBC 驱动 Class.forName(JDBC_DRIVER); // 打开链接 System.out.println("连接数据库..."); conn = DriverManager.getConnection(DB_URL,USER,PASS); // 执行查询 System.out.println("实例化Statement对象..."); stmt = conn.createStatement(); String sql; sql = "SELECT * FROM login";//搜索login表,实际须填入数据库的表名 ResultSet rs = stmt.executeQuery(sql); // 展开结果集数据库 while(rs.next()){ // 通过字段检索 int id = rs.getInt("id");//得到“id”列的值,类型需要与数据库的数值类型相同 String password = rs.getString("password");//得到“password”列的值,类型需要与数据库的数值类型相同 //如有其他列可以选择添加 // 输出数据 System.out.print("ID: " + id); System.out.print(", 密码: " + password); System.out.print("n"); } // 完成后关闭 rs.close(); stmt.close(); conn.close(); }catch(SQLException se){ // 处理 JDBC 错误 se.printStackTrace(); }catch(Exception e){ // 处理 Class.forName 错误 e.printStackTrace(); }finally{ // 关闭资源 try{ if(stmt!=null) stmt.close(); }catch(SQLException se2){ }// 什么都不做 try{ if(conn!=null) conn.close(); }catch(SQLException se){ se.printStackTrace(); } } System.out.println("Goodbye!"); } }
运行后的结果:
数据库message中login表的数据:
数据库就连接完成了
查询数据的代码:
Connection conn = null; Statement stmt = null; try{ // 注册 JDBC 驱动 Class.forName(JDBC_DRIVER); // 打开链接 System.out.println("连接数据库..."); conn = DriverManager.getConnection(DB_URL,USER,PASS); // 执行查询 System.out.println("实例化Statement对象..."); stmt = conn.createStatement(); String sql; sql = "SELECT * FROM login";//搜索login表,实际须填入数据库的表名 ResultSet rs = stmt.executeQuery(sql); // 展开结果集数据库 while(rs.next()){ // 通过字段检索 int id = rs.getInt("id");//得到“id”列的值,类型需要与数据库的数值类型相同 String password = rs.getString("password");//得到“password”列的值,类型需要与数据库的数值类型相同 //如有其他列可以选择添加 // 输出数据 System.out.print("ID: " + id); System.out.print(", 密码: " + password); System.out.print("n"); } // 完成后关闭 rs.close(); stmt.close(); conn.close(); }catch(SQLException se){ // 处理 JDBC 错误 se.printStackTrace(); }catch(Exception e){ // 处理 Class.forName 错误 e.printStackTrace(); }finally{ // 关闭资源 try{ if(stmt!=null) stmt.close(); }catch(SQLException se2){ }// 什么都不做 try{ if(conn!=null) conn.close(); }catch(SQLException se){ se.printStackTrace(); } } System.out.println("数据查询成功");
如有错误
欢迎指出
下一篇:
总结篇: