一.前言
寫這個文章的緣由是爲了鞏固本身鏈接數據庫步驟
但願對初次學習數據庫的朋友有所幫助
二.MySQL數據庫鏈接
數據庫鏈接步驟加載驅動程序,獲取數據庫鏈接對象
2.1驅動jar包
2.2代碼
package cn.sg.xx.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Main {
public static void main(String[] args) throws SQLException, ClassNotFoundException {
try {
//數據庫驅動參數
String Driver = "com.mysql.cj.jdbc.Driver";//"com.mysql.jdbc.Driver"-->驅動地址已通過時
//鏈接的URL,數據庫名爲test
String URL = "jdbc:mysql://localhost:3306/test?serverTimezone=UTC&characterEncoding=utf8";//後邊的參數serverTimezone-->指定時區,characterEncoding-->編碼格式
//數據庫用戶名
String Username = "root";
//數據庫密碼
String Password = "123";
//加載MySQL驅動程序
Class.forName(Driver);
//與MySQL數據庫創立鏈接
Connection con = null;
con = DriverManager.getConnection(URL, Username, Password);
} catch (ClassNotFoundException e) {
System.out.println("驅動程序沒有找到!");
} catch (SQLException e) {
System.out.println("SQL異常!");
}
}
}
三.增長表數據
String sql = "INSERT INTO test(id,NAME,sex) VALUES(?,?,?);";
建議寫成這種語法,指定插入的列,能夠防止表字段修改後,程序報錯
使用PreparedStatement 進行sql語句的裝配,能夠防止sql注入
3.1代碼
package cn.sg.xx.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class Main {
public static void main(String[] args) {
try {
//數據庫驅動參數
String Driver = "com.mysql.cj.jdbc.Driver";//"com.mysql.jdbc.Driver"-->驅動地址已通過時
//鏈接的URL,數據庫名爲test
String URL = "jdbc:mysql://localhost:3306/test?serverTimezone=UTC&characterEncoding=utf8";//後邊的參數serverTimezone-->指定時區,characterEncoding-->編碼格式
//數據庫用戶名
String Username = "root";
//數據庫密碼
String Password = "123";
//加載MySQL驅動程序
Class.forName(Driver);
//與MySQL數據庫創立鏈接
Connection con = null;
con = DriverManager.getConnection(URL, Username, Password);
//sql語句裝配,?爲佔位符
String sql = "INSERT INTO test(id,NAME,sex) VALUES(?,?,?);";
PreparedStatement stmt = con.prepareStatement(sql);
stmt.setInt(1, 2);
stmt.setString(2, "張三");
stmt.setString(3, "男");
stmt.executeUpdate();
//執行完數據庫的操做有指令以後要釋放資源,不然會致使內存的溢出
stmt.close();
con.close();
} catch (ClassNotFoundException e) {
System.out.println("驅動程序沒有找到!");
} catch (SQLException e) {
System.out.println("SQL異常!");
}
}
}
四.刪除表數據
刪除表數據的操做和插入操做基本相同,只有sql語句不一樣
4.1代碼
String sql = "DELETE FROM test WHERE id = 1";
PreparedStatement stmt = con.prepareStatement(sql);
stmt.executeUpdate();
五.修改表數據
同插入數據同樣,僅僅修改sql語句爲
UPDATE test SET sex = '女' WHERE id = 1
六.查找表數據
表數據的查找須要比上邊的插入、刪除操做多關閉ResoultSet資源
ResoultSet rs;
rs.close();
6.1代碼
//查找表數據
String sql = "SELECT * FROM test";
PreparedStatement stmt = con.prepareStatement(sql);
ResultSet rs = stmt.executeQuery();
//從 rs 獲取數據
while (rs.next()) {
//rs.getMetaData().getColumnCount() --> 獲取表的列數
for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
String colunmName = rs.getMetaData().getColumnName(i);//獲取列名
Object temp = rs.getString(i);//獲取該列對應元素的值
System.out.print(colunmName + "-->" + temp + "\t");
}
System.out.println();
七.所有代碼
package cn.sg.xx.test;
import java.sql.*;
public class Main {
public static void main(String[] args) throws SQLException {
Connection con = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
//數據庫驅動參數
String Driver = "com.mysql.cj.jdbc.Driver";//"com.mysql.jdbc.Driver"-->驅動地址已通過時
//鏈接的URL,數據庫名爲test
String URL = "jdbc:mysql://localhost:3306/test?serverTimezone=UTC&characterEncoding=utf8";//後邊的參數serverTimezone-->指定時區,characterEncoding-->編碼格式
//數據庫用戶名
String Username = "root";
//數據庫密碼
String Password = "123";
//加載MySQL驅動程序
Class.forName(Driver);
//與MySQL數據庫創立鏈接
con = DriverManager.getConnection(URL, Username, Password);
//sql語句裝配,?爲佔位符
String sql = "INSERT INTO test(id,NAME,sex) VALUES(?,?,?);";
stmt = con.prepareStatement(sql);
stmt.setInt(1, 2);
stmt.setString(2, "張三");
stmt.setString(3, "男");
stmt.executeUpdate();
//刪除表數據
sql = "DELETE FROM test WHERE id = 1";
stmt = con.prepareStatement(sql);
stmt.executeUpdate();
//修改表數據
sql = "UPDATE test SET sex = '女' WHERE id = 1";
stmt = con.prepareStatement(sql);
stmt.executeUpdate();
//查找表數據
sql = "SELECT * FROM test";
stmt = con.prepareStatement(sql);
rs = stmt.executeQuery();
//從 rs 獲取數據
while (rs.next()) {
//rs.getMetaData().getColumnCount() --> 獲取表的列數
for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
String colunmName = rs.getMetaData().getColumnName(i);//獲取列名
Object temp = rs.getString(i);//獲取該列對應元素的值
System.out.print(colunmName + "-->" + temp + "\t");
}
System.out.println();
}
//執行完數據庫的操做有指令以後要釋放資源,不然會致使內存的溢出
} catch (ClassNotFoundException e) {
System.out.println("驅動程序沒有找到!");
} catch (SQLException e) {
System.out.println("SQL異常!");
} finally {
//關閉資源
if (rs != null)
rs.close();
if (stmt != null)
stmt.close();
if (con != null)
con.close();
}
}
}