Results 1 to 2 of 2
Thread: Can't connect using jdbc!!!
- 11-26-2008, 10:06 PM #1
Member
- Join Date
- Nov 2008
- Posts
- 6
- Rep Power
- 0
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
Main.javaJava 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); } } }
and what I get is this: :(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"); ....
I just can't figure out what's wrong :confused:Java 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 really need some help on this.
any working code snippet or advice ... anything
- 11-26-2008, 10:49 PM #2
Member
- Join Date
- Nov 2008
- Posts
- 6
- Rep Power
- 0
Silly MEEE!!!
Well.... I forgot to put a "result.next()" right after the "ResultSet rs = st.executeQuery(SQL_Query);" and so Java was trying to read the row-before-the-first.... oupsy!! :o
It's amazing when forgetting something so simple can cause you such big trouble.
The positive thing that came out of this though is that I finally downloaded the new mysql-connector... :DLast edited by mikep_bluefish; 11-26-2008 at 10:53 PM.
Similar Threads
-
how connect
By herfnai in forum JDBCReplies: 1Last Post: 08-18-2008, 06:34 AM -
connect JDBC to offline database
By nancyhung in forum JDBCReplies: 1Last Post: 04-11-2008, 11:04 PM -
How to use JDBC Template classes to control basic JDBC processing and error handling
By Java Tip in forum Java TipReplies: 0Last Post: 04-01-2008, 10:17 AM -
Using JDBC to connect to ORACLE database
By Java Tip in forum Java TipReplies: 0Last Post: 02-10-2008, 11:27 AM -
How to use JDBC Template classes to control basic JDBC processing and error handling
By JavaBean in forum Java TipReplies: 0Last Post: 09-28-2007, 12:56 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks