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 11:37 AM. Reason: Added code-tags.
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...
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks