here's ma code its from netbeans.
/*first i'm using two Jbuttons,
*the first buttons reads the selected database to connect to from a JComboBox; 1 being MySql
* it thus retrives the username, passwd and database to connect and executes the connection
*
*the second is to run the entered queries but for development purposes I have had to define a standard query
*this query should return results as shown in previous thread but now in a JTextArea called myans.
*/
private void mycActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mycActionPerformed
String myname = new String(""+ uname.getText()) ;
String mypasswd = new String(""+ passwd.getText()) ;
String mydb = new String(""+ dbname.getText()) ;
//Ceck if the username has been provided
if (myname!=""){
//parameters to connect to the database
int chos = mychoices.getSelectedIndex();
if (chos == 1){
try {
String driver = "com.mysql.jdbc.Driver";
Class.forName(driver);
String url = "jdbc:mysql://localhost/"+mydb;
java.sql.DriverManager.getConnection( url, myname, mypasswd );
}
catch( Exception x ) {
x.printStackTrace();
}
}
else if (chos == 2){
JOptionPane.showMessageDialog(this,"You are not allowed to connect to PostgeSQL","Configuration Error",JOptionPane.ERROR_MESSAGE);
}
else if (chos == 3){
JOptionPane.showMessageDialog(this,"You are not allowed to connect to Oracle","Configuration Error",JOptionPane.ERROR_MESSAGE);
}
}
//If no username show error!
else if (myname == ""){
JOptionPane.showMessageDialog(this,"Please give atlease a user name","ENTRY PROBLEM!",JOptionPane.ERROR_MESSAGE);
}
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
myans.setText("Here comes the results \n");
StringBuffer message = new StringBuffer("");
Connection conn = null;
Statement stmt = null;
try{
//StringBuffer message = new StringBuffer("");
stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT name, owner FROM pet");
//ResultSetMetaData metaData = resultSet.getMetaData();
//int numberOfColumns = metaData.getColumnCount();
ResultSetMetaData metaData = rs.getMetaData();
int numberOfColumns = metaData.getColumnCount();
myans.setText("Column count is :" + numberOfColumns +"\n");
/* while ( rs.next() )
{
for ( int i = 1; i <= numberOfColumns; i++ )
myans.setText("Column count is :" + rs.getObject(i) +"\n");
//message.append(rs.getObject(i));
//message.append("\n");
}*/
while (rs.next()) {
//String entry = rs.getString(1);
String s = rs.getString("name");
message.append(s);
String f = rs.getString("owner");
message.append(f);
}
String hdss = message.toString();
myans.append(hdss);
//myans.setText(hdss);
}
catch (SQLException e) {
e.printStackTrace();
System.exit( 1 );
}
finally // ensure statement and connection are closed properly
{
try {
stmt.close();
conn.close();
} // end try
catch ( Exception exception ) {
exception.printStackTrace();
System.exit( 1 );
} // end catch
} // end finally
}
i've also tried to make sure that the username is not null and give an error message, I've used if with !="" and !=null but none works.
For the Jtextarea i've even tried StringBuffer.
So pls help me.