Results 1 to 7 of 7

Thread: JDBC Driver

  1. #1
    Ursula is offline Member
    Join Date
    Aug 2010
    Location
    Madrid
    Posts
    3
    Rep Power
    0

    Default JDBC Driver

    Im new to java, and yes i have searched endlessly to find a solution to my problem... I have the following code:

    Java Code:
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    public class EjemploJDBC {
    	public static void main(String args[])
    		// Servidor (máquina)
    		String host = "";
    
    		// Sevidor (puerto): el puerto por defecto es el 3306
    		String port = "3306";
    
    		// Usuario
    		String user = "";
    
    		// Contraseña
    		String passwd = "";
    
    		// Base de datos a utilizar
    		String db = "ejemplo";
    
    		// Representa una conexión a la base de datos
    		Connection conn = null;
    
    		// Permite ejecutar una sentencia o comando en la base de datos
    		Statement stmt = null;
    
    		// Representa a los resultados de ejecutar una consulta en la base de datos
    		ResultSet rs = null;
    
    		try {
    
    			Class.forName("com.mysql.jdbc.Driver").newInstance();
    
    			String url = "jdbc:mysql://" + host + ":" + port + "/" + db;
    
    			conn = DriverManager.getConnection(url, user, passwd);
    			stmt = conn.createStatement();
    
    			int result = stmt.executeUpdate("CREATE TABLE tabla_ejemplo (id INT, nombre VARCHAR(25), primary key(id))");
    
    			result = stmt.executeUpdate("INSERT INTO tabla_ejemplo VALUES(1, 'Un nombre cualquiera')");
    
    			result = stmt.executeUpdate("INSERT INTO tabla_ejemplo VALUES(2, 'Otro nombre')");
    
    			rs = stmt.executeQuery("SELECT * FROM tabla_ejemplo");
    
    			while (rs.next()) {
    
    				System.out.println("id=" + rs.getInt("id") + " nombre=" + rs.getString("nombre"));
    
    			}
    
    			rs = stmt.executeQuery("SELECT COUNT(*) as valor FROM tabla_ejemplo");
    
    			if (rs.next()) {
    
    				System.out.println("Num de entradas=" + rs.getInt("valor"));
    
    			}
    
    
    
    		}
    
    		catch (Exception e) {
    
    			e.printStackTrace();
    
    		}
    
    		finally {
    
    			try {
    
    				if (rs != null) rs.close();
    				if (stmt != null) stmt.close();
    				if (conn != null) conn.close();
    			}
    
    			catch (SQLException e) {
    
    				System.out.println("Error al liberar recursos");
    
    			}
    		}
    	}
    
    }
    And i compile it the following way:
    ursula@ursula-laptop:~/Desktop/JAVA$ javac EjemploJDBC.java
    ursula@ursula-laptop:~/Desktop/JAVA$ java -cp /$HOME/Desktop/JAVA/ EjemploJDBC
    java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    at java.net.URLClassLoader$1.run(URLClassLoader.java: 202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.j ava:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:3 07)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:2 48)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:169)
    at EjemploJDBC.main(EjemploJDBC.java:45)

    I have the jar file in the path and the path is correct...
    im in ubuntu.

    Any ideas?

  2. #2
    mangstadt is offline Member
    Join Date
    Jun 2010
    Location
    USA
    Posts
    19
    Rep Power
    0

    Default

    Try adding the JDBC driver JAR file to the classpath when you run the program. So something like:

    java -cp /$HOME/Desktop/JAVA/mysql.jar EjemploJDBC

    You have to declare each individual JAR file in the classpath...just declaring a directory won't work.

  3. #3
    arjun_duddu is offline Member
    Join Date
    Apr 2010
    Posts
    7
    Rep Power
    0

    Default

    Moderator edit: hijack post deleted. User warned.
    Last edited by Fubarable; 08-23-2010 at 06:47 PM.

  4. #4
    Ursula is offline Member
    Join Date
    Aug 2010
    Location
    Madrid
    Posts
    3
    Rep Power
    0

    Default

    I tried the following

    ursula@ursula-laptop:~/Desktop/JAVA$ java -cp /$HOME/Desktop/JAVA/mysql-connector-java-5.1.5-bin.jar: EjemploJDBC
    com.mysql.jdbc.exceptions.jdbc4.CommunicationsExce ption: Communications link failure

    Last packet sent to the server was 1 ms ago.
    at sun.reflect.NativeConstructorAccessorImpl.newInsta nce0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInsta nce(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newI nstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Construc tor.java:513)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:40 6)
    at com.mysql.jdbc.SQLError.createCommunicationsExcept ion(SQLError.java:1074)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(Connecti onImpl.java:2104)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImp l.java:729)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connect ion.java:46)
    at sun.reflect.NativeConstructorAccessorImpl.newInsta nce0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInsta nce(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newI nstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Construc tor.java:513)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:40 6)
    at com.mysql.jdbc.ConnectionImpl.getInstance(Connecti onImpl.java:302)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonReg isteringDriver.java:283)
    at java.sql.DriverManager.getConnection(DriverManager .java:582)
    at java.sql.DriverManager.getConnection(DriverManager .java:185)
    at EjemploJDBC.main(EjemploJDBC.java:49)
    Caused by: java.net.SocketException: Network is unreachable
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(PlainSocketImpl .java:333)
    at java.net.PlainSocketImpl.connectToAddress(PlainSoc ketImpl.java:195)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.j ava:182)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.j ava:366)
    at java.net.Socket.connect(Socket.java:529)
    at java.net.Socket.connect(Socket.java:478)
    at java.net.Socket.<init>(Socket.java:375)
    at java.net.Socket.<init>(Socket.java:218)
    at com.mysql.jdbc.StandardSocketFactory.connect(Stand ardSocketFactory.java:256)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:276)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(Connecti onImpl.java:2027)
    ... 12 more



    I had tried something similar before but without the : and it didnt work. But now what i understand from what i get is that the server is not responding which is a completely different problem.

  5. #5
    mangstadt is offline Member
    Join Date
    Jun 2010
    Location
    USA
    Posts
    19
    Rep Power
    0

    Default

    Quote Originally Posted by Ursula View Post
    But now what i understand from what i get is that the server is not responding which is a completely different problem.
    Yep that's what it looks like.

  6. #6
    Ursula is offline Member
    Join Date
    Aug 2010
    Location
    Madrid
    Posts
    3
    Rep Power
    0

    Default

    thanks!

    How do i mark this as solved?

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,655
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by arjun_duddu View Post
    hii all!!
    i have a problem when i run a jdbc program, it gives me an error by oracle.jdbc.Driver.OracleDriver....... please help me out
    Database:
    Oracle 9i

    platform:
    Windows XP

    please help me out!!
    Please don't try to hijack someone else's thread; either start your own thread or follow this one if you think you have a (nearly) identical problem.

    kind regards,

    Jos

Similar Threads

  1. creating .jar with jdbc driver?
    By Godsent in forum New To Java
    Replies: 2
    Last Post: 04-16-2009, 05:58 PM
  2. DB2 JDBC Driver upgrade
    By sanssan in forum JDBC
    Replies: 2
    Last Post: 09-23-2008, 04:52 PM
  3. Jdbc Driver For Oracle
    By Swamipsn in forum New To Java
    Replies: 0
    Last Post: 08-14-2007, 04:31 AM
  4. Help with JDBC driver
    By Daniel in forum JDBC
    Replies: 2
    Last Post: 07-03-2007, 08:16 PM

Posting Permissions

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