開始使用基于java的JDBC技術來連接mysql進行msyql資料庫簡單的CRUD操作
下載對應mysql驅動包
這里我創建maven專案基于maven下載
<!--mysql 驅動-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.21</version>
</dependency>
<!---->
連接資料庫
/*
* jdbc:mysql: 是指JDBC連接方式;
*127.0.0.1: 是指你的本機地址;
* 3306 SQL資料庫的埠號;
* mysql 就是你要連接的資料庫的名字,
* characterEncoding 指定資料庫連接的編碼方式
* com.mysql.jdbc.Driver 注冊資料庫驅動 過時
* com.mysql.cj.jdbc.Driver 新的資料庫驅動
*/
public Connection getConnection() {
//mysql 連接url
String url = "jdbc:mysql://localhost:3306/kenx_test?characterEncoding=utf-8";
//資料庫用戶名
String userName = "root";
//資料庫密碼
String passWord = "root";
try {
Class.forName("com.mysql.cj.jdbc.Driver"); //注冊資料庫驅動
Connection conn = DriverManager.getConnection(url, userName, passWord); //獲取資料連接
System.out.println("資料庫連接成功");
return conn;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
進行資料添加操作
public int ins() {
Connection connection = null;
Statement statement = null;
String sql = "";
try {
//獲取資料連接
connection = getConnection();
//獲取發送sql指令執行sql物件
statement = connection.createStatement();
StringBuffer sbf = new StringBuffer("insert into user (id, name, age, email, manager_id, create_time)");
String id = String.valueOf(System.currentTimeMillis());
Timestamp dateTime = Timestamp.valueOf(DateUtil.now());
sbf.append(" values (" + id + ",'kenx',24,'[email protected]',155065178165505,'" + dateTime + "')");
sql = sbf.toString();
System.out.println("執行sql" + sql);
//執行成功回傳1
int success = statement.executeUpdate(sql);
return success;
} catch (Exception e) {
e.printStackTrace();
return 0;
}finally {
//執行完資料庫操作后記得關閉資料庫連接資源
try{
statement.close();
connection.close();
}catch (SQLException e){
e.printStackTrace();
}
}
}
進行資料庫的查詢操作
public void select() {
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
String sql = "select * from user limit 0,1";
try {
//獲取資料連接
connection = getConnection();
//獲取發送sql指令執行sql物件
statement = connection.createStatement();
//回傳查詢結果集用于保存資料庫查詢內容
rs = statement.executeQuery(sql);
//遍歷結果集拿到資料
while (rs.next()) {
System.out.println("id" + "\t" + rs.getString("id"));
System.out.println("name" + "\t" + rs.getString("name"));
System.out.println("age" + "\t" + rs.getString("age"));
System.out.println("email" + "\t" + rs.getString("email"));
System.out.println("manager_id" + "\t" + rs.getString("manager_id"));
System.out.println("create_time" + "\t" + rs.getString("create_time"));
}
} catch (Exception e) {
e.printStackTrace();
}finally {
//執行完資料庫操作后記得關閉資料庫連接資源
try{
rs.close();
statement.close();
connection.close();
}catch (SQLException e){
e.printStackTrace();
}
}
}
進行資料庫更新操作
public int update() {
Connection connection = null;
Statement statement = null;
String sql = "update user\n" +
"set manager_id =155065512602445\n" +
"where id=1597890374878";
try {
//獲取資料連接
connection = getConnection();
//獲取發送sql指令執行sql物件
statement = connection.createStatement();
//更新成功回傳1
int success = statement.executeUpdate(sql);
return success;
} catch (Exception e) {
e.printStackTrace();
return 0;
}finally {
//執行完資料庫操作后記得關閉資料庫連接資源
try{
statement.close();
connection.close();
}catch (SQLException e){
e.printStackTrace();
}
}
}
進行資料庫洗掉操作
public int del(){
Connection connection = null;
Statement statement = null;
String sql = "delete from user where id='1597890374878'";
try {
//獲取資料連接
connection = getConnection();
//獲取發送sql指令執行sql物件
statement = connection.createStatement();
//洗掉成功回傳1
int success = statement.executeUpdate(sql);
return success;
} catch (Exception e) {
e.printStackTrace();
return 0;
}finally {
//執行完資料庫操作后記得關閉資料庫連接資源
try{
statement.close();
connection.close();
}catch (SQLException e){
e.printStackTrace();
}
}
}
完整專案案例
點擊這里 github
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/63396.html
標籤:Java
上一篇:java基礎-01:dos命令
