如何在我的Java应用程序中使用XAMPP MySQL数据库? [英] How to use XAMPP MySQL database with my Java Application?

查看:380
本文介绍了如何在我的Java应用程序中使用XAMPP MySQL数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在过去的几个月中,我将XAMPP与MySQL数据库一起使用,这些数据库是通过localhost上的phpMyAdmin创建和修改的,用于我的大学JavaEE项目。 MySQL数据库和Apache服务器是从XAMPP控制面板启动的。一切正常。

I have used in the past few months XAMPP with MySQL database(s), which were created and modified with phpMyAdmin on localhost, for my university JavaEE projects. The MySQL database and Apache server are started from the XAMPP Control Panel. Everything went fine.

现在,我正在使用JavaFX / Scene Builder / FXML开发自己的Java桌面应用程序,我想使用数据库来存储和加载由JavaFX处理的各种信息。

Now I am developing my own Java Desktop Application using JavaFX/Scene Builder/FXML and I want to use a database to store and load various information processed by the Java Application through JDBC.

问题是,当我完成Java应用程序并进行部署时,如何在本地启动MySQL数据库,而无需手动使用XAMPP控制面板

The question is, how to start the MySQL database on localhost, without using the XAMPP Control Panel manually, when I finish my Java Application and deploy it as stand alone program and start it just from a single shortcut?

任何使程序成为快捷方式的方法还可以在其之前/同时在我的PC上启动MySQL数据库启动Java应用程序?也许在Java代码中有办法做到这一点?也许是其他方式,我不知道?

Any way to make the shortcut of the program also start the MySQL database on my PC before/while it starts the Java Application? Or maybe there is a way to do that inside the Java code? Or maybe some other way, that is not known to me?

我不是严格确定仅使用XAMPP / MySQL / phpMyAdmin设置,而是已经全部安装了在我的PC上,我知道如何使用它,因此是迄今为止最简单的解决方案。因此,如果有一些针对家庭/小型应用程序的更好的方法/数据库设置,请随时提出一些建议:)。我完全不确定XAMPP设置是否可以实现我的目标。

I am not strictly determined on using only the XAMPP/MySQL/phpMyAdmin setup, it is just already all installed on my PC and I know how to work with it, thus the easiest solution so far. So if there is some better way/database setup for home/small applications, please feel free to suggest some :). I am not sure at all if what I want to do is possible with the XAMPP setup.

侧面说明:我坚持使用localhost DB而不是Java的序列化/反序列化,因为我希望该应用程序独立于Internet连接,并且有机会将准备好的数据库转移到在线数据库中,如果我决定将来这样做的话。

Side note: I persist on using localhost DB instead of Serialisation/Deserialisation with Java, because I want the Application to be independent of internet connection and yet have the opportunity to have a ready DB to be transferred to an online DB, if such I decide to do it in the future.

推荐答案

您可以这样做-


  1. 要连接到数据库

 import java.sql.Connection;
 import java.sql.DriverManager;
 import java.sql.Statement;
 import java.sql.Connection;
 import java.sql.DriverManager;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.sql.Statement;

 Statement statement;

 //function to connect to the xampp server      
 public void DatabaseConnect(){
     try {
        Connection conn= DriverManager.getConnection("jdbc:mysql://localhost/javafx","root",""); 
        /*here javafx is the name of the database and root is the username of
         your xampp server and the field which is blank is for password. 
         Because I haven't set the password. I have left it blank*/
         statement = conn.createStatement();
         System.out.print("Database Connected");
     } catch (Exception e) {
         System.out.print("Database Not Connected");
     }
  }


下面给出的是您连接到数据库后可以执行的各种操作。

//for inserting data
public void insert(){
    try{
        String insertquery = "INSERT INTO `tablename`(`field1`, `field2`) VALUES ('value1', 'value2'";
        statement.executeUpdate(insertquery);
        System.out.print("Inserted");
    } catch(Exception e){
        System.out.print("Not Inserted");
    }
}

 //for viewing data
 public void view(){
    try {
        String insertquery = "select * from `table_name` where field = 'value1'";
        ResultSet result = statement.executeQuery(insertquery);
        if(result.next()){
            System.out.println("Value " + result.getString(2));
            System.out.println("Value " + result.getString(3));
        }
    } catch (SQLException ex) {
        System.out.println("Problem To Show Data");
    }
 }

 //to update data
 public void update(){
    try {
        String insertquery = "UPDATE `table_name` set `field`='value',`field2`='value2' WHERE field = 'value'";
        statement.executeUpdate(insertquery);
        System.out.println("Updated")
    } catch (SQLException ex) {
        System.out.println(ex.getMessage());
    }
 }

//to delete data
public void delete(){
     try {
        String insertquery = "DELETE FROM `table_name` WHERE field = 'value'";
        statement.executeUpdate(insertquery);
        System.out.println("Deleted");
    } catch (SQLException ex) {
        System.out.println(ex.getMessage());
    }
 }

此外,不要忘记添加<$ 系统库中的c $ c> JAR 文件。在此示例中,我使用了 mysql-connector-java-5.1.46

Also, don't forget to add the JAR file in your system library. For this example, I have used mysql-connector-java-5.1.46

这篇关于如何在我的Java应用程序中使用XAMPP MySQL数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆