JavaFX程序初次運(yùn)行創(chuàng)建數(shù)據(jù)庫并執(zhí)行建表SQL詳解
在我的第一個(gè)JavaFX程序完成安裝的時(shí)候才突然發(fā)現(xiàn),不能要用這個(gè)軟件還要手動(dòng)執(zhí)行Sql來建表吧?
于是我的想法是在Main程序中執(zhí)行時(shí)檢測數(shù)據(jù)庫連接狀況,如果沒有檢測到數(shù)據(jù)庫或者連接異常,那么出現(xiàn)錯(cuò)誤提示,如果數(shù)據(jù)庫連接沒有問題那么自動(dòng)創(chuàng)建數(shù)據(jù)庫并執(zhí)行建表Sql進(jìn)行初始化。
package oa.util;
import java.io.IOException;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import org.apache.ibatis.jdbc.ScriptRunner;
import com.ibatis.common.resources.Resources;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
public class CreateMySqlDatabase {
public static void createDatabase() throws SQLException {
Connection conn;
conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/", "root", "1234");
Statement stmt = (Statement) conn.createStatement();
String sql = "CREATE DATABASE UTILITY";
stmt.executeUpdate(sql);
}
public static void executeSql() throws IOException, SQLException {
Properties props = Resources.getResourceAsProperties("mysql.properties");
String url = props.getProperty("jdbc.url");
String username = props.getProperty("jdbc.username");
String password = props.getProperty("jdbc.password");
Connection conn = (Connection) DriverManager.getConnection(url, username, password);
ScriptRunner runner = new ScriptRunner(conn);
runner.setErrorLogWriter(null);
runner.setLogWriter(null);
runner.runScript(Resources.getResourceAsReader("sql/utility.sql"));
conn.close();
System.out.println("==SUCCESS==");
}
}
需要用到 ibatis-common-2.jar讀取mysql.properties文件中的JDBC信息。
MAIN做判斷:
try {
DriverManager.getConnection("jdbc:mysql://localhost:3306/utility", "root", "1234");
isError = false;
} catch (MySQLSyntaxErrorException e) {
primaryStage.setTitle("正在創(chuàng)建Utility數(shù)據(jù)庫……");
Label error = new Label("正在創(chuàng)建Utility數(shù)據(jù)庫……");
error.setFont(new Font("Cambria", 100));
Pane pane = new Pane();
pane.getChildren().add(error);
Scene scene = new Scene(pane);
primaryStage.setScene(scene);
primaryStage.show();
try {
CreateMySqlDatabase.createDatabase();
CreateMySqlDatabase.executeSql();
isError = false;
primaryStage.close();
} catch (SQLException | IOException e1) {
primaryStage.close();
e1.printStackTrace();
}
} catch (SQLException se) {
Thread.sleep(3000);
primaryStage.close();
se.printStackTrace();
}
if (!isError) {
run();
}
}
public static void main(String[] args) {
launch(args);
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java 中二進(jìn)制轉(zhuǎn)換成十六進(jìn)制的兩種實(shí)現(xiàn)方法
這篇文章主要介紹了Java 中二進(jìn)制轉(zhuǎn)換成十六進(jìn)制的兩種實(shí)現(xiàn)方法的相關(guān)資料,需要的朋友可以參考下2017-06-06
SpringBoot靜態(tài)資源映射,圖片無法實(shí)時(shí)訪問問題及解決
文章介紹了Spring Boot中靜態(tài)資源映射配置,解決了圖片上傳后無法實(shí)時(shí)訪問的問題,通過配置虛擬路徑,將訪問路徑映射到指定的物理路徑,解決了圖片無法實(shí)時(shí)顯示的問題2025-02-02
Spring中的@EnableConfigurationProperties使用方式以及作用詳解
這篇文章主要介紹了Spring中的@EnableConfigurationProperties使用方式以及作用詳解,使用了?@ConfigurationProperties?注解的配置類生效,將該類注入到?IOC?容器中,交由?IOC?容器進(jìn)行管理,此時(shí)則不用再配置類上加上@Component,需要的朋友可以參考下2024-01-01
Java泛型與數(shù)據(jù)庫應(yīng)用實(shí)例詳解
這篇文章主要介紹了Java泛型與數(shù)據(jù)庫應(yīng)用,結(jié)合實(shí)例形式詳細(xì)分析了java繼承泛型類實(shí)現(xiàn)增刪改查操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-08-08
剖析Java中在Collection集合中使用contains和remove為什么要重寫equals
這篇文章主要介紹了Collection集合的contains和remove方法詳解remove以及相關(guān)的經(jīng)驗(yàn)技巧,通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09

