-1

我已经完成了我的第一个访问数据库并允许我远程访问机器的 Java 程序。

当我从我自己的机器(它还托管它连接到的数据库)运行它时,这可以正常工作。但是,如果我在另一台机器上使用它并连接到数据库,则程序在执行数据库任务时会严重挂起。

我预计这将是一个小小的等待,但有时可能需要几分钟才能完成手头的任务。

这是它所执行的任务之一。

    public void ChangeSite() throws Exception{

    ConnectionPoolS demo = new ConnectionPoolS();
    DataSource dataSource = demo.setUp();

    Connection conn = null;
    PreparedStatement stmt = null;



    String Site = (String)Sites.getSelectedItem();
    jProgressBar.setIndeterminate(true);
    jProgressBar.setStringPainted(true);
    jProgressBar.setString("Searching...");
    int sRecordNo; 
     sRecordNo = 0;
String IpAddress = null, IpRange = null, Atvisionpw = null, Port = null, Practice = null, Surgery = null;
try { 


    conn = dataSource.getConnection();
    stmt = conn.prepareStatement("SELECT * FROM Customer WHERE Practice = '"+Site+"' and BRANCH = 'N'");
    ResultSet rs = stmt.executeQuery( );

    while (rs.next()) {
    sRecordNo = sRecordNo + 1;
    Surgery = rs.getString("SURGERY");
    Practice = rs.getString("PRACTICE");
    IpAddress = rs.getString("IPADDRESS");
    Port = rs.getString("PORT");
    Atvisionpw = rs.getString("ATVISIONPW");
    IpRange = rs.getString("IPRANGE");
}
    if (sRecordNo == 1){
    SiteIp.setText(IpAddress);
    Password.setText(Atvisionpw);
    PcIp.setText(IpRange);
    }
    else if (sRecordNo > 1){
       Multisite();
    }
    jProgressBar.setIndeterminate(false);
    jProgressBar.setString("");
stmt.close();
conn.close();
}

这一切都在 derby 数据库上。

我做错了什么?我错过了什么?有没有办法我可以对此进行某种性能检查,以找出在运行数据库任务时究竟是什么原因导致它挂起,因为这都是内部网络上的所有内部网络,我不希望等待我是!

编辑:在 Stefan 的帮助下(谢谢),我发现使用断点似乎在运行线路后挂起

conn = dataSource.getConnection(); 

之后的下一行是现在

stmt = conn.prepareStatement("SELECT * FROM Customer WHERE Practice = '"+Site+"' and BRANCH = 'N'");

但它只在运行连接线后挂起,所以这是否意味着与物理数据库的连接导致挂起?

Edit2:我现在设置了一个连接池,但是连接时它仍然挂起,我现在有点茫然!,不同的引擎会提供更好的结果吗?

4

2 回答 2

1

经过一番搜索(你的结论是创建连接本身就是性能猪),我找到了这个. 连接池比自己设置连接性能要好得多。它也更容易使用,因为您不必自己管理连接。请记住,完成后关闭连接,以便可以将其返回到池中以供将来使用。

于 2013-09-17T09:27:57.453 回答
0

use of PreparedStatement over statement may improve performance up to little bit. some details about PreparedStatement

PreparedStatement is a class in java.sql package and allows Java programmer to execute SQL queries by using JDBC package. You can get PreparedStatement object by calling connection.prepareStatement() method.SQL queries passed to this method goes to Database for pre-compilation if JDBC driver supports it. If it doesn't than pre-compilation occurs when you execute prepared queries. Prepared Statement queries are pre-compiled on database and there access plan will be reused to execute further queries which allows them to execute much quicker than normal queries generated by Statement object.

How to use PreparedStatement

public class PreparedStmtExample {

    public static void main(String args[]) throws SQLException {
Class.forName("com.mysql.jdbc.Driver");
        Connection conn =     DriverManager.getConnection("jdbc:mysql://localhost:3306/DataBase","username","Password");
        PreparedStatement preStatement = conn.prepareStatement("select *  from tableName where Name=?");
        preStatement.setString(1, "test");

        ResultSet result = preStatement.executeQuery();

        while(result.next()){
            System.out.println("Loan Type: " + result.getString("loan_type"));
        }       
    }
}
于 2013-09-17T08:34:14.930 回答