/*
* mysql_actions.java
*
* Created on 06. Dezember 2006, 14:02
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package mysql_test;
/**
*
* @author ulrichju
*/
import java.beans.*;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.SQLException;
import java.sql.ResultSet;
import java.sql.*;
import java.util.HashMap;
import javax.swing.*;
public class mysql_actions {
private Connection conn;
private Statement stmt;
private ResultSet rSet;
private String[] Result;
private int arraySize;
private int ColumnCount;
private String[] ColumnName;
private HashMap hashDaten = new HashMap();
/** Creates a new instance of mysql_actions */
public mysql_actions() {
conn = getConnection("db_account");
//getResult(executeQuery("SELECT * FROM orders"));
}
//*******************************************************************************************************
public static void main(String[] args) {
mysql_actions ma = new mysql_actions();
}
//*******************************************************************************************************
public Connection getConnection(String dbName) {
Connection con = null;
// Create a new MySQL Driver Instance
try {
System.out.println( "Create a new MySQL Driver Instance");
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
} catch ( Exception e ) {
System.out.println( "ERROR: the MySQL Driver couldn't be created");
e.printStackTrace();
}
try {
System.out.println("Connect to MySQL " + dbName);
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/" + dbName, "root", "admin");
}
catch(SQLException sqe) {
System.out.println("Couldn't acces MySQL database " + dbName);
sqe.printStackTrace();
}
return con;
}
//*******************************************************************************************************
public ResultSet executeQuery(String query) {
try {
stmt = conn.createStatement();
} catch (SQLException ex) {
ex.printStackTrace();
}
Object[] options = { "OK" };
JOptionPane.showOptionDialog(null, query, "Warning", JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE, null, options, options[0]);
try {
rSet = stmt.executeQuery(query);
} catch (SQLException ex) {
ex.printStackTrace();
}
setColumnCount(rSet);
System.out.println("size: " + getColumnCount());
return rSet;
}
//*******************************************************************************************************
public void setColumnCount(ResultSet rS) {
try {
ResultSetMetaData meta = rS.getMetaData();
ColumnCount = meta.getColumnCount();
setColumnName(meta);
}
catch (SQLException ex) {
ex.printStackTrace();
}
}
//*******************************************************************************************************
public int getColumnCount() {
return ColumnCount;
}
//*******************************************************************************************************
public void setColumnName(ResultSetMetaData meta) {
ColumnName = new String[getColumnCount()];
int j=1;
for(int i=0; i<getColumnCount(); i++) {
try {
ColumnName[i] = meta.getColumnName(j);
j++;
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}
//*******************************************************************************************************
public String[] getColumnName() {
return ColumnName;
}
//*******************************************************************************************************
public HashMap getResult(ResultSet rSet) {
int i=1;
try {
while(rSet.next()) {
for(int j=1; j<=getColumnCount(); j++) {
hashDaten.put(Integer.toString(i), rSet.getString(j));
System.out.println(rSet.getString(j));
i++;
}
}
}
catch(Exception e) {
e.printStackTrace();
}
return hashDaten;
}
//*******************************************************************************************************
public void endConn() {
try {
rSet.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
try {
stmt.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
try {
conn.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}