Results 1 to 6 of 6
Thread: java mysql ubuntu problem
- 12-04-2012, 09:14 PM #1
Member
- Join Date
- Dec 2012
- Posts
- 2
- Rep Power
- 0
java mysql ubuntu problem
Hello!
Thanks for letting participate.
I've got the following problem:
Installed: Ubuntu 10.04, Java 7 and a MySQL Server
I now want to connect to the Server with Java but get the following error from the following code.
I've tried to fix it by browsing google but it didn't help.
I already say thank you for helping me.
Max
Java Code:java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost/test java.lang.NullPointerException java.lang.NullPointerException
Java Code:import java.sql.*; import javax.sql.*; import java.io.*; import java.util.*; /**This Class queries the specified databases with the specified parameters and returns all relevant information*/ public class DBQuery{ private String dburl; private String user; private String password; private String query; private ArrayList<ArrayList<Object>> publicdata = new ArrayList<ArrayList<Object>>(); private int actualcolumn = 1; private int actualrow = 1; private Connection cn; /**Database Query for MYSQL Databases*/ DBQuery(String host, String database, String user, String password, String query){ this.cn = null; String dburl = "jdbc:mysql://"+host+"/"+database; this.dburl = dburl; this.user = user; this.password = password; this.query = query; getData(); } /**Return the data of a database table in an ArrayList*/ void getData(){ ArrayList<ArrayList<Object>> data = new ArrayList<ArrayList<Object>>(); data.clear(); this.publicdata.clear(); ResultSet rs = dataFromDB(); try{ rs.last(); int rows = rs.getRow(); rs.beforeFirst(); ResultSetMetaData rsmd = rs.getMetaData(); ArrayList<Object> names = new ArrayList<Object>(); for(int i = 0; i < rsmd.getColumnCount(); i++){ names.add(rsmd.getColumnName(i+1)); } data.add(names); for(int i = 1; i <= rsmd.getColumnCount(); i++){ ArrayList<Object> a = new ArrayList<Object>(); rs.beforeFirst(); for(int e = 1; e <= rows; e++){ rs.next(); a.add(rs.getString(i)); if(e==rows){ data.add(a); } } } } catch(Exception e){ System.out.println(e); } this.publicdata = data; try{ cn.close(); } catch(Exception e){ System.out.println(e); } } /**Returns the Value on the specified position of the result of this Query*/ public Object getValue(int column, int row){ this.actualcolumn = column; this.actualrow = row; return publicdata.get(column-1+1).get(row-1); } /**Returns the total Number of Columns of the result of this Query*/ public int getTotalNumberOfColumns(){ return publicdata.get(0).size(); } /**Returns the total Number of Rows of the result of this Query*/ public int getTotalNumberOfRows(){ return publicdata.get(1).size(); } /**Acts like a cursor, which shows the postion of the column, the has been asked for the latest*/ public int getActualColumn(){ return actualcolumn; } /**Acts like a cursor, which shows the postion of the row, the has been asked for the latest*/ public int getActualRow(){ return actualrow; } /**Sets the cursor of the Row*/ public void setRow(int x){ if(x > 1){ this.actualrow=x; } } /**Sets the cursor of the Column*/ public void setColumn(int x){ if(x>1){ this.actualcolumn=x; } } /**Return the name of the specific column 'x'*/ public String getNameofColumn(int x){ return (String) publicdata.get(0).get(x-1); } void printOut(){ ArrayList<ArrayList<Object>> datb = publicdata; ArrayList<Object> columnnames = new ArrayList<Object>(); columnnames.add(datb.get(0)); datb.remove(0); System.out.print(columnnames.get(0)); System.out.println(); for(int i = 0; i < datb.size(); i++){ for(int e = 0; e < datb.get(i).size(); e++){ System.out.print(datb.get(i).get(e)+ " | "); } System.out.println(); } } /**Executes the Database Query*/ ResultSet dataFromDB(){ Statement st = null; ResultSet rs = null; try{ cn = DriverManager.getConnection(dburl, user, password); st = cn.createStatement(); rs = st.executeQuery(query); } catch(Exception e){ System.out.println(e); } return rs; } }
- 12-04-2012, 10:02 PM #2
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
Re: java mysql ubuntu problem
Do you have the mysql-connector jar in your classpath? Usually before establishing the database connection, I would expect to see the following:
Java Code:Class.forName("com.mysql.jdbc.Driver");
- 12-05-2012, 05:08 AM #3
Re: java mysql ubuntu problem
Moved from New to Java
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 12-05-2012, 09:29 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
- 12-06-2012, 03:16 PM #5
Member
- Join Date
- Dec 2012
- Posts
- 2
- Rep Power
- 0
Re: java mysql ubuntu problem
I've added this line in the dataFromDB Method now, but it still doesnt work
- 12-06-2012, 03:23 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Similar Threads
-
Problem INSERTing java.sql.Date into MySQL table
By wmd in forum JDBCReplies: 3Last Post: 10-18-2012, 03:00 PM -
in java (csv into mysql) making problem in date formate
By raj.mscking@gmail.com in forum New To JavaReplies: 1Last Post: 06-08-2012, 10:08 AM -
Problem running Simple Hello world program in Ubuntu
By teete in forum New To JavaReplies: 4Last Post: 04-24-2012, 08:08 PM -
Using mysql in Java on ubuntu
By dearvivekkumar in forum JavaServer Pages (JSP) and JSTLReplies: 3Last Post: 11-30-2011, 09:36 AM -
mysql classpath ubuntu problem
By munish in forum JDBCReplies: 18Last Post: 08-22-2010, 01:55 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks