Koziołek napisał(a)
Skorzystaj z pojedynczego połączenia. Zaimplementuj dostęp do bazy jako singleton:
dzieki za zainteresowaniem tematem.... więc zrobiłem tak:
Factory.java
package pl.maksi.nitro.supply.database;
import com.mysql.jdbc.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Lusaczek
*/
public class Factory
{
protected static Connection connection = null;
private static String error;
public static Connection getConnection()
{
try {
if (connection == null || connection.isClosed())
{
createNewConnection();
}
return connection;
} catch (SQLException ex) {
Logger.getLogger(Factory.class.getName()).log(Level.SEVERE, null, ex);
return null;
}
}
static void close() throws SQLException
{
if( connection != null)
connection.close();
}
private static void createNewConnection()
{ try
{
Class.forName("org.gjt.mm.mysql.Driver");
//outp.println("Połączenie nawiązane!");
}
catch(ClassNotFoundException e)
{
error = "Błąd przy ładowaniu sterownika bazy: " + e;
//System.exit(-1);
}
try
{
String adr = java.util.ResourceBundle.getBundle("pl/maksi/nitro/propaties/general").getString("DBhost");
String port = java.util.ResourceBundle.getBundle("pl/maksi/nitro/propaties/general").getString("DBport");
String base = java.util.ResourceBundle.getBundle("pl/maksi/nitro/propaties/general").getString("DBbase");
String username = java.util.ResourceBundle.getBundle("pl/maksi/nitro/propaties/general").getString("DBuser");
String password = java.util.ResourceBundle.getBundle("pl/maksi/nitro/propaties/general").getString("DBpass");
//System.out.print("jdbc:mysql://"+ adr +":"+ port +"/"+ base +"");
connection = (Connection) DriverManager.getConnection("jdbc:mysql://"+ adr +":"+ port +"/"+ base +"?useUnicode=true&characterEncoding=UTF-8&useOldUTF8Behavior=true", username, password);
}
catch(SQLException e)
{
error = "Nie można nawiązać połączenia z bazą: " + e;
//System.exit(-1);
}
}
}
Maintenance.java
public class Maintenance
{
private String error;
private int insertId;
private boolean transaction;
private Connection getCon()
{
return Factory.getConnection();
}
public ResultSet result(String sql)
{ ResultSet result = null;
try
{
Statement statement = (Statement) getCon().createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
ResultSet rs = statement.executeQuery(sql);
result = rs;
}
catch(SQLException e)
{ this.error = "Błąd podczas przetwarzania danych: " + e; }
catch(Exception e)
{ this.error = "Błąd WYNIKU - ogólny: " + e; }
return result;
}
public boolean insert(String sql)
{ boolean re = false;
try
{
Statement statement = (Statement) getCon().createStatement();
if(statement.executeUpdate(sql)==1)
{
re = true;
insertId = setInsertId();
}
}
catch(SQLException e)
{ this.rollback(); this.error = "Błąd podczas MYSQL:insert: " + e; }
catch(Exception e)
{ this.rollback(); this.error = "Błąd WYNIKU - ogólny: " + e; }
return re;
}
//itd...
}
poczym modyfikuje dodawanie rekordu...
public boolean impInsert(String cas, String num, String[] add)
{ boolean re = true;
int type = 0;
if(add[1].equals("OB"))
type = 1;
else if(add[1].equals("ONW"))
type = 2;
Maintenance m = new Maintenance();
String sql = "INSERT INTO counting_case (office, type, name, number, dateImport) VALUES ("+add[2]+", "+type+", '"+cas+"', '"+num+"', NOW())";
m.insert(sql);
return re;
}
efektem... tego jest....
http://zapodaj.net/b361429c0ef8.jpg.html
http://zapodaj.net/b361429c0ef8.jpg.html
http://zapodaj.net/7174fb8ea2b0.jpg.html
czyli nie jest źle efekt jest widoczny... tyle tylko że strasznie wolno a dysk prawie czerwony tak sie kreci ...zerknij prosze czy dobrze zrozumialem singleton i czy da sie to zoptymalizowac?