MSQL数据库(Jdbc)
1.mysql安装
https://blog.****.net/weixin_40928946/article/details/86598493
2.Navicat安装
链接:https://pan.baidu.com/s/1jIDeBY5h1duuknmbtMUqng
提取码:5vsy
Navicat绿色版 ***:NAVH-WK6A-DMVK-DKW3
(若过期可以百度其他)
(1)汉化界面
(2)连接数据库mysql :连接名自己设置,密码为博主开机密码,连接IP为本机
(3)连接成功
(4) 打开数据库:双击MySQL5.7
(5)双击数据库名字打开与关闭数据库
(6)新建数据库
(7)数据库内建立表:新建表 --点击保存--命名t_user
(8)查看对象信息:常规 DDL
(9)添加信息
3. JDBC
java database connection 为java开发者使用的数据库提供统一编程接口。是java程序与数据库系统通信的标准API
JDBC访问的流程:
(1)JDBC的driver接口
(2)JDBC的Statementr接口
1‘’ Statement 类
package com.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class Demo02 {
public static void main(String[] args) {
try {//加载驱动类
Class.forName("com.mysql.jdbc.Driver");
// DriverManager建立连接 比较耗时
Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/wuhaiboo","root","whb9");
//不常用 避免SQL注入 不安全
Statement stmt = conn.createStatement();
String sql = "insert into t_user (username, pwd, regTime) values ('zhao liu','123456',now())";
stmt.execute(sql);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
2‘ ’PrepareStatement类
package com.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class Demo03 {
public static void main(String[] args) {
try {//加载驱动类
Class.forName("com.mysql.jdbc.Driver");
// DriverManager建立连接 比较耗时
Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/wuhaiboo","root","whb9");
String sql = "insert into t_user (username, pwd, regTime) values (?,?,now())";//占位符
//防止sql注入问题
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1,"gaoqi");//占位符第一个参数 setString可以用setObject替换,不用考虑类型
ps.setObject(2, "12348");//占位符第二个参数
System.out.println("插入一行记录");
ps.execute();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
3‘’ CallableStatement类
(3)接口常用方法
execute():运行语句,返回是否有结果集 true/false
executeQuery():运行select语句,返回Result结果 插入几行记录
package com.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Demo03 {
public static void main(String[] args) {
try {//加载驱动类
Class.forName("com.mysql.jdbc.Driver");
// DriverManager建立连接 比较耗时
Connection conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/wuhaiboo","root","whb9");
String sql = "select * from t_user where id>?";//id大于2的全部取出
PreparedStatement ps = conn.prepareStatement(sql);
ps.setObject(1, 2);
ResultSet rs = ps.executeQuery();
while(rs.next()){
System.out.println(rs.getInt(1)+"----"+rs.getString(2));
}
// String sql = "insert into t_user (username, pwd, regTime) values (?,?,now())";//占位符
// //防止sql注入问题
// PreparedStatement ps = conn.prepareStatement(sql);
// ps.setString(1,"gaoqi");//占位符第一个参数 setString可以用setObject替换,不用考虑类型
// ps.setObject(2, "12348");//占位符第二个参数
// System.out.println("插入一行记录");
// ps.execute();
// int count = ps.executeUpdate();
// System.out.println(count);
//
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
executeUpdate():运行insert/update/delete操作,返回更新的行数