java数据库设计工具_Java课程设计---创建数据库工具类
1、傳統的數據庫操作
package com.java.mysql;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
/**
*
* Title: db.java
* @author daxiang
* @version 1.0 創建時間:2018年5月22日 上午8:00:22
*/
public class Add {
public static void main(String[] args) {
Connection con;// 聲明Connection對象
String driver = "com.mysql.jdbc.Driver";// 驅動程序名
String url = "jdbc:mysql://localhost:3306/salary";// URL指向要訪問的數據庫名mysql
String user = "root";// MySQL配置時的用戶名
String password = "123";// MySQL配置時的密碼
try {
Class.forName(driver);// 加載驅動程序
con = DriverManager.getConnection(url, user, password);// 使用getConnection()方法,連接MySQL數據庫!!
if (!con.isClosed())
System.out.println("成功連接mysql數據庫");
// 2.創建statement類對象,用來執行SQL語句!!
Statement statement = con.createStatement();
// 要執行的SQL語句
String sql = "insert into admin (id,username,password) values(2,'test','123')";
statement.execute(sql);
statement.close();
con.close();
} catch (ClassNotFoundException e) {
System.out.println("無法加載驅動");
e.printStackTrace();// 數據庫驅動類異常處理
} catch (SQLException e) {
e.printStackTrace();// 數據庫連接失敗異常處理
} catch (Exception e) {
e.printStackTrace();
}
}
}
2、自己封裝db工具DbUtil.java
由于增加、刪除、更新在代碼上存在大量的重復,所以現將數據庫操作作為工具獨立出來
package com.system.utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.system.common.AppConstants;
/**
* 數據庫連接類
*
* @author daxiang
*
*/
public class DbUtil {
// 定義連接對象
private Connection connection;
private Statement statement;
/**
*
* @throws Exception
*/
public DbUtil() throws Exception {
this.connection = getCon();
this.statement = connection.createStatement();
}
/**
* 獲取數據庫連接
*
* @return 數據庫連接Connection
* @throws Exception
*/
public Connection getCon() throws Exception {
Class.forName(AppConstants.JDBC_DRIVER);
Connection con = DriverManager.getConnection(AppConstants.JDBC_URL, AppConstants.JDBC_USERNAME,
AppConstants.JDBC_PASSWORD);
return con;
}
/**
* 關閉數據庫連接
*
* @param con
* @throws SQLException
*/
public void closeCon(Connection con) throws SQLException {
if (con != null) {
con.close();
}
}
/**
* 更新
*
* @param sql
* @return
* @throws SQLException
*/
public boolean update(String sql) throws SQLException {
return statement.execute(sql);
}
/**
* 添加
*
* @param sql
* @return
* @throws SQLException
*/
public boolean add(String sql) throws SQLException {
return statement.execute(sql);
}
/**
* 刪除
*
* @param sql
* @return
* @throws SQLException
*/
public boolean delete(String sql) throws SQLException {
return statement.execute(sql);
}
/**
* 查詢
* @param sql
* @return
* @throws SQLException
*/
public ResultSet query(String sql) throws SQLException{
return statement.executeQuery(sql);
}
}
將可能修改的參數獨立到一個AppConstants類中,便于修改
/**
* 項目名:student
* 作者:daxiang
* 時間:2018.6.1
*/
package com.system.common;
/**
* 模塊說明: 常量
*
*/
public class AppConstants {
// jdbc
public static final String JDBC_URL = "jdbc:mysql://127.0.0.1:3306/db_student?useUnicode=true&characterEncodeing=UTF-8";
public static final String JDBC_USERNAME = "root";
public static final String JDBC_PASSWORD = "123";
public static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
// login view
public static final String LOGIN_TITLE = "登錄界面";
public static final String LOGIN_USERNAME = "用戶名";
public static final String LOGIN_PASSWORD = "密碼";
public static final String LOGIN = "登錄";
public static final String RESET = "重置";
}
總結
以上是生活随笔為你收集整理的java数据库设计工具_Java课程设计---创建数据库工具类的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java socket 推送机制_Jav
- 下一篇: java判断或_Java 条件判断