Results 1 to 6 of 6
  1. #1
    maxxx12345 is offline Member
    Join Date
    Dec 2012
    Posts
    2
    Rep Power
    0

    Default 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;
    
        }
    }

  2. #2
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    307
    Rep Power
    6

    Default 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");

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: java mysql ubuntu problem

    Moved from New to Java

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: java mysql ubuntu problem

    Quote Originally Posted by StormyWaters View Post
    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");
    That's not been necessary for a while now.
    The Driver gets registered automatically.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    maxxx12345 is offline Member
    Join Date
    Dec 2012
    Posts
    2
    Rep Power
    0

    Default Re: java mysql ubuntu problem

    I've added this line in the dataFromDB Method now, but it still doesnt work

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: java mysql ubuntu problem

    That line is not the solution.

    The first part of StormyWaters reply is.
    You don't have the driver jar file in your classpath.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 3
    Last Post: 10-18-2012, 04:00 PM
  2. in java (csv into mysql) making problem in date formate
    By raj.mscking@gmail.com in forum New To Java
    Replies: 1
    Last Post: 06-08-2012, 11:08 AM
  3. Replies: 4
    Last Post: 04-24-2012, 09:08 PM
  4. Using mysql in Java on ubuntu
    By dearvivekkumar in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 11-30-2011, 10:36 AM
  5. mysql classpath ubuntu problem
    By munish in forum JDBC
    Replies: 18
    Last Post: 08-22-2010, 02:55 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •