1.打开Eclipse,点击File新建项目
2.导入Mysql的包
(1)选择Bulid Path --Add External Archiyes
二、建立数据库并查询数据1.建立连接
2.右键打开连接
3.新建数据库
4.新建查询
5.创建表
6.添加数据
10.查看数据
三.JDBC 连接MySQL 数据库并查询数据
1.新建包test
2.新建类
3.输入代码运行
package test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mysql.jdbc.Statement;
public class MySQLDemo {
static final String driverName="org.gjt.mm.mysql.Driver";
static final String dbUrl="jdbc:mysql://localhost:3306/test";
static final String userName="root";
static final String password="xyp20000921";
public static void main(String[] args) {
// TODO Auto-generated method stub
// MySQL 8.0 以下版本 - JDBC 驱动名及数据库 URL
Connection conn = null;
Statement stmt = null;
try{
// 注册 JDBC 驱动
Class.forName(driverName);
// 打开链接
System.out.println("连接数据库...");
conn = DriverManager.getConnection(dbUrl,userName,password);
// 执行查询
System.out.println(" 实例化Statement对象...");
stmt = (Statement) conn.createStatement();
String sql;
sql = "SELECt id, name, url FROM websites";
ResultSet rs = stmt.executeQuery(sql);
// 展开结果集数据库
while(rs.next()){
// 通过字段检索
int id = rs.getInt("id");
String name = rs.getString("name");
String url = rs.getString("url");
// 输出数据
System.out.print("ID: " + id);
System.out.print(", 站点名称: " + name);
System.out.print(", 站点 URL: " + url);
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!");
}
}
4.结果



