|
||||||
| Java Help Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
Hi guys.I have a problem.I just created the connection class and it works perfect for sure.Now i have a problem.I have a mysql database named tbl_login and in it there is 2 columns which are Username and password. Now in the java program i created 2 jtextfields and i want to view the username and password i created into the database into the 2 jtextfields. How im gonna do it? Below you will find my connection coding, tks:
Code:
package project1;
import com.mysql.jdbc.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mysql.jdbc.Statement;
import javax.swing.JOptionPane;
public class COnnection {
static Connection connection;
static Statement statement;
static ResultSet myResult = null;
public static ResultSet getResultsFromQuery(String sqlQry) {
connection = null;
statement = null;
myResult = null;
connection = getConnection();
try {
//instanciation of statement
statement = (Statement)connection.createStatement();
//executing the statement
myResult = statement.executeQuery(sqlQry);
} catch (SQLException f) {
//displaying an error popup window
JOptionPane.showMessageDialog(null, f.toString());
f.printStackTrace();
}
return myResult;
}
private static Connection getConnection() {
//declaration of the connection
connection = null;
try {
//load the j/connector driver
Class.forName("com.mysql.jdbc.Driver");
//creating the connection to the database
//connection properties
String server = "localhost";
String database = "mydatabase";
String url = "jdbc:mysql://" + server + "/" + database;
String username = "renezamm";
String password = "123";
connection =
(Connection)DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException e) {
System.out.println("could not find the database driver");
} catch (SQLException e) {
System.out.println("Could not connect to the database");
}
// since this method returns a connection we are goint to return the created conenction
return connection;
}
/**
* this method is used to execute a query
* @param myQuery enter the query you wish to execute here
*/
public static void excecuteQuery(String myQuery) {
connection = getConnection();
try {
statement = (Statement)connection.createStatement();
statement.execute(myQuery);
} catch (SQLException e) {
System.out.print(e);
}
}
/**
* this method is used to see the records within a table
* @param tableName the name of the table of which you want to see the data
* @return a resultset containing all requested data of the given table
*/
public static ResultSet viewRecords(String tableName) {
connection = getConnection();
try {
//instanciation of statement
statement = (Statement)connection.createStatement();
//executing the statement
String sqlQry = "Select * from " + tableName;
myResult = statement.executeQuery(sqlQry);
} catch (SQLException f) {
//displaying an error popup window
JOptionPane.showMessageDialog(null, f.toString());
f.printStackTrace();
}
return myResult;
}
public COnnection() {
}
Last edited by v0id; 05-27-2007 at 02:37 PM. Reason: Added code-tags. |
| Sponsored Links |
|
|
|
|||
|
I haven't read all your code, still I can try and give you few hints:
1. Why do you include com.mysql.xxx ... Usually you need java.sql.xxx alternatives. ( xxx is Connection and Statement ) 2. Consider separating sql queries from visualization - make a class that will query you database for the information and will have getters and another class that will contain the GUI code and will use the getters of the first class - this will make it easier to read your code and fix bugs/mistakes (If you are not in a hurry read about Model-View-Controller). Sorry, I don't have time right now to supply a working code fragment... |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Java:Tutorial - Data Types | John | Java Tutorials | 6 | 07-02-2007 10:16 PM |
| Fetching Data from a Form Generated Website | pclark2 | General Programming | 5 | 05-11-2007 07:24 AM |
| MYSQL CheatSheet - A must for MySQL Users | reachpradeep | Database & Database Programming | 1 | 03-03-2007 04:05 PM |
| MySQL Resources | dirkfirst | Database & Database Programming | 1 | 05-25-2006 03:31 AM |
| WingedPanther | ........ | 2753.6 |
| Xav | ........ | 2704 |
| Brandon W | ........ | 1702.32 |
| John | ........ | 1207.73 |
| marwex89 | ........ | 1175.24 |
| morefood2001 | ........ | 966.05 |
| dcs | ........ | 655.75 |
| Steve.L | ........ | 475.59 |
| orjan | ........ | 418.58 |
| Aereshaa | ........ | 383.54 |