JavaFX程序初次運(yùn)行創(chuàng)建數(shù)據(jù)庫(kù)并執(zhí)行建表SQL詳解
在我的第一個(gè)JavaFX程序完成安裝的時(shí)候才突然發(fā)現(xiàn),不能要用這個(gè)軟件還要手動(dòng)執(zhí)行Sql來(lái)建表吧?
于是我的想法是在Main程序中執(zhí)行時(shí)檢測(cè)數(shù)據(jù)庫(kù)連接狀況,如果沒有檢測(cè)到數(shù)據(jù)庫(kù)或者連接異常,那么出現(xiàn)錯(cuò)誤提示,如果數(shù)據(jù)庫(kù)連接沒有問題那么自動(dòng)創(chuàng)建數(shù)據(jù)庫(kù)并執(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ù)庫(kù)……"); Label error = new Label("正在創(chuàng)建Utility數(shù)據(jù)庫(kù)……"); 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); }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
出現(xiàn)次數(shù)超過一半(50%)的數(shù)
給出n個(gè)數(shù),需要我們找出出現(xiàn)次數(shù)超過一半的數(shù),下面小編給大家分享下我的實(shí)現(xiàn)思路及關(guān)鍵代碼,感興趣的朋友一起學(xué)習(xí)吧2016-07-07spring boot 實(shí)現(xiàn)配置多個(gè)DispatcherServlet最簡(jiǎn)單方式
這篇文章主要介紹了spring boot 實(shí)現(xiàn)配置多個(gè)DispatcherServlet最簡(jiǎn)單方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2021-01-01kotlin java 混合代碼 maven 打包實(shí)現(xiàn)
這篇文章主要介紹了kotlin java 混合代碼 maven 打包實(shí)現(xiàn),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03SpringMVC統(tǒng)一異常處理實(shí)例代碼
這篇文章主要介紹了SpringMVC統(tǒng)一異常處理實(shí)例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11Netty事件循環(huán)主邏輯NioEventLoop的run方法分析
這篇文章主要介紹了Netty事件循環(huán)主邏輯NioEventLoop的run方法分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-03-03基于idea 的 Java中的get/set方法之優(yōu)雅的寫法
這篇文章主要介紹了基于idea 的 Java中的get/set方法之優(yōu)雅的寫法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2019-01-01