java 连接 MySQL 步骤
不是干货不受关注!!!
一,去maven下载有关jdbc的包
链接: maven官网.
搜索
选择对应的jdbc包
拖入项目中,添加为库
package com.huiju.Jdbc;
import java.sql.*;
public class Deom01 {
private static String driver = "com.mysql.jdbc.Driver";//驱动类位置
private static String url = "jdbc:mysql://localhost:3306/myfdb";//数据库位置
private static String user = "root";//用户名
private static String password = "admin";//密码
public static void main(String[] args) {
Connection conn = null;
Statement sta = null;
ResultSet res = null;
try {
Class.forName(driver);//加载驱动
conn = DriverManager.getConnection(url, user, password);//开辟连接通道
sta = conn.createStatement();//负责数据传输
res = sta.executeQuery("SELECT *FROM`student`");//执行Sql语句
while (res.next()) {//xxx.next(): 遍历判断有无下一条数据
String studentName = res.getString("studentName");//解析赋值数据
int gradeId = res.getInt("gradeId");//解析赋值数据
Date bornDate = res.getDate("bornDate");//解析赋值数据
System.out.println(studentName + "---" + gradeId + "---" + bornDate);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {//关闭进程
if(res!=null)
res.close();
if(sta!=null)
sta.close();
if(conn!=null)
conn.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
如果感觉够干,就留下点啥吧!!!