私は初心者のJavaとMySQLのユーザーです。MySQL データベースで実装された Java プログラムを作成するのはこれが初めてです。別のコンピューターにあるが同じローカル ネットワーク内にあるクライアントでクエリを実行すると、なぜそんなに待ち時間が長くなるのか疑問があります。
私のプログラム構造:
レストランPOSアプリです。私の DB と私の POS プログラムは別のコンピューターにあります。私のクエリ コマンドはすべてプログラム内に組み込まれています。私のDBには、1つのスキーマといくつかのテーブルしかありません。これらのテーブルに格納される行データはごくわずかです。
問題:
プログラムのボタンを押してクエリを実行し、DB から行を選択すると、tt が実行されて何かが返されるまでに少なくとも 1 秒かかります。そこで、プログラムをデータベースサーバーと同じコンピューターに移動しようとしました。以前の 10 倍の速度で実行されます。ああ、神様。何が起こっている。クエリがローカル ネットワークを介して実行されたときに、なぜそれほど時間がかかるのかわかりません。誰かが理由と、可能であればリアルタイムにする別の方法を説明できますか?
私のサンプルクエリコード:
Connection co = null;
PreparedStatement ps= null;
try{
co = myconnection.getConnection();
String insert = "INSERT INTO `R`.`order` (name, firstCourse, secondCourse, thirdCourse, seafood, other, dessert, drink, price, quantity, note, type, position , time_in ,subtable, tid , aid ) ";
insert += "VALUE ( '"+itemName+"','"+itemFirst+"','"+itemSecond+"','"+itemThird+"','"+itemSeafood+"','"+itemOther+"','"+itemDessert+"','"+itemDrink+"','"+itemPrice+"','"+num+"','"+notes+"','"+type+"','"+position+"','"+dateTime+"','"+subcheck+"','"+tableID+"','"+userID+"')";
ps = co.prepareStatement(insert);
ps.executeUpdate();
this.updateJTable();
}catch(Exception e){
System.out.println("error occur when insert item:"+e);
}finally{
try {
ps.close();
co.close();
} catch (SQLException e) { /* ignored */}
}
これは私の接続クラスです:
import java.sql.*;
public class myConnection {
public static Connection getConnection() throws Exception {
String url = "jdbc:mysql://192.168.1.2:3306/r";
String user = "root";
String pass = "";
Connection connection = null;
Class.forName("com.mysql.jdbc.Driver").newInstance();
try{
connection = DriverManager.getConnection(url, user, pass);
}catch (SQLException ex) {
ex.printStackTrace();
}
return connection;
}
public static void closeConnection(Connection connection) throws SQLException {
//if (connection != null && !connection.isClosed()) {
connection.close();
//}
}
public void closeRs(ResultSet resultSet) throws SQLException{
//if (resultSet != null) {
resultSet.close();
//}
}
public void closePs(PreparedStatement preparedStatement) throws SQLException{
//if (preparedStatement != null) {
preparedStatement.close();
//}
}
}