天天看點

安卓mysql資料庫連接配接工具類_自定義JDBC工具類 連接配接MySQL資料庫

需要用到的jar包:mysql-connector-java-5.1.43-bin.jar

1.把連接配接資料庫需要的資訊,都儲存在一個檔案中,這個檔案是一個properties檔案

driver=com.mysql.jdbc.Driver

url=jdbc:mysql://localhost:3306/javaee1707?useSSL=true

user=root

password=123456

代碼實作

public class JDBCUtil {

private static String url = null;

private static String user = null;

private static String password = null;

private static String driverClass = null;

private static InputStream in = null;

//利用靜态代碼塊的特征,在類檔案加載到記憶體的時候,就會執行在靜态代碼塊裡面的代碼

static {

try {

//1. 讀取配置檔案資訊 讀取properties檔案

Properties props = new Properties();

//如果一個properties檔案加載到記憶體中,需要借助于IO流

in = new FileInputStream("./src/db.properties");

//2. 利用Properties裡面的load方法加載檔案

props.load(in);

//3. 可以通過Properties類對象,擷取到想要的資料

url = props.getProperty("url");

user = props.getProperty("user");

password = props.getProperty("password");

driverClass = props.getProperty("driver");

//4. 加載類檔案

Class.forName(driverClass);

} catch (IOException | ClassNotFoundException e) {

// TODO: handle exception

e.printStackTrace();

System.out.println("驅動加載失敗");

} finally {

//關閉檔案連接配接

if (in != null) {

try {

in.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

public static Connection getConnection() {

Connection conn = null;

try {

conn = DriverManager.getConnection(url, user, password);

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return conn;

}

public static void close(Connection conn, Statement st) {

try {

if (st != null) {

st.close();

}

if (conn != null) {

conn.close();

}

} catch (SQLException e) {

// TODO: handle exception

e.printStackTrace();

//糖衣炮彈

throw new RuntimeException(e);

}

}

//關閉帶有結果集的查詢語句資源

public static void close(Connection conn, Statement st, ResultSet set) {

try {

if (st != null) {

st.close();

}

if (set != null) {

set.close();

}

if (conn != null) {

conn.close();

}

} catch (SQLException e) {

// TODO: handle exception

e.printStackTrace();

//糖衣炮彈

throw new RuntimeException(e);

}

}

}

3.通過自定義JDBC工具類來連接配接資料庫

public class Demo2 {

//建立表格

@Test

public void createTable() {

Statement st = null;

//1. 通過已經封裝好的JDBC工具類,擷取到資料庫的連接配接對象

Connection conn = JDBCUtil.getConnection();

try {

//2. 擷取Statement,SQL語句運輸者,将SQL語句運輸到MySQL裡面,讓MySQL運作

st = conn.createStatement();

//3. 準備SQL語句

String sql = "create table WOW(heroID int not null primary key auto_increment,heroName char(30))";

//4. 通過Statement執行SQL語句

int count = st.executeUpdate(sql);

//5. 檢視建立的結果

System.out.println("影響的行數:" + count);

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

// 使用Statement執行DML語句

@Test

public void testInsert() {

Statement st = null;

//1. 建立資料庫連接配接

Connection conn = JDBCUtil.getConnection();

try {

//2. 擷取到Statement

st = conn.createStatement();

//3. 準備SQL語句

String sql = "insert into WOW(heroName) values('薩滿')";

//4. 通過Statement執行SQL語句

int count = st.executeUpdate(sql);

//5. 影響的行數

System.out.println("count = " + count);

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {

//conn是連接配接資料庫的資源,st是應用程式到MySQL直接的SQL語句運輸者

//這兩個都算是資源,是以都需要關閉

try {

if (st != null) {

st.close();

}

if (conn != null) {

conn.close();

}

} catch (SQLException e) {

// TODO: handle exception

e.printStackTrace();

}

}

}

/ /使用Statement查詢資料庫中資料

@Test

public void testSelect() {

Connection conn = null;

Statement st = null;

ResultSet set = null; //查詢語句傳回的結果集對象

try {

//1. 擷取資料庫連接配接對象

conn = JDBCUtil.getConnection();

//2. 擷取Statement

st = conn.createStatement();

//3. 準備SQL語句

String sql = "select * from WOW";

//4. 執行SQL語句 【擷取查詢結果集】

set = st.executeQuery(sql);

//5. Result next() getXXX(String 字段名) XXX表示不同的資料類型,

//根據目前的代碼需求 使用不同的資料類型

while (set.next()) {

int heroID = set.getInt("heroID");

String heroName = set.getString("heroName");

System.out.println(heroID + ":" + heroName);

}

} catch (SQLException e) {

// TODO: handle exception

e.printStackTrace();

} finally {

JDBCUtil.close(conn, st, set);

}

}

}