Can't connect using jdbc!!!
Hi
I'm trying to connect to mySQL via jdbc and I end up with a bunch of exceptions :confused:
My code is separated in a "Connector" class
which is imported to my app.
The source goes like this:
Connector.java
Code:
package jdbcConnect;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Connector {
public String host = null;
public String port = null;
public String database = null;
public String username = null;
public String password = null;
Connection conn;
public Connector(){
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException ex) {
Logger.getLogger(Connector.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void mySQL_open(){
try {
conn = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database + "?user=" + username + "&password=" + password);
} catch (SQLException ex) {
Logger.getLogger(Connector.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void excecute_SQL(String SQL_Query){
try {
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(SQL_Query);
System.out.println(rs.getString("name"));
} catch (SQLException ex) {
Logger.getLogger(Connector.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Main.java
Code:
....
Connector conn = new Connector();
conn.database = "test";
conn.host = "localhost";
conn.port = "3306";
conn.username = "root";
conn.password = "rootpswd";
conn.mySQL_open();
conn.excecute_SQL("select * from users");
....
and what I get is this: :(
Code:
init:
deps-jar:
Compiling 1 source file to /home/user/Documents/Netbeans_Projects/jdbcApp/build/classes
compile:
run:
Nov 26, 2008 10:45:31 PM jdbcConnect.Connector excecute_SQL
SEVERE: null
java.sql.SQLException
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)
at com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImpl.java:815)
at com.mysql.jdbc.ResultSetImpl.getStringInternal(ResultSetImpl.java:5528)
at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5448)
at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5488)
at jdbcConnect.Connector.excecute_SQL(Connector.java:48)
at jdbcapp.JdbcView.<init>(JdbcView.java:96)
at jdbcapp.JdbcApp.startup(JdbcApp.java:19)
...
I just can't figure out what's wrong :confused:
I really need some help on this.
any working code snippet or advice ... anything