Java e MySQL[Resolvido]

1 resposta Resolvido
javamysql
ClockWork

criei um código para conectar a meu banco de dados localhost porem ele da o erro Class.forname

Código:
import java.sql.*;

public class Base {
	
   static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
   static final String DB_URL = "jdbc:mysql://localhost/teste";

   static final String USER = "root";
   static final String PASS = "senha";
   
   public static void main(String[] args) {
   Connection conn = null;
   Statement stmt = null;
   try{
      
      Class.forName("com.mysql.jdbc.Driver");

      
      System.out.println("conectando...");
      conn = DriverManager.getConnection(DB_URL,USER,PASS);

      
      System.out.println("Acessesando dados...");
      stmt = conn.createStatement();
      String sql;
      sql = "SELECT * FROM teste";
      ResultSet rs = stmt.executeQuery(sql);

      
      while(rs.next()){
         
         int id  = rs.getInt("id");
         String c1 = rs.getString("coluna1");
         String c2 = rs.getString("coluna2");
         String c3 = rs.getString("coluna3");

         
         System.out.print(id + " ");
         System.out.print(c1  + " ");
         System.out.print(c2  + " ");
         System.out.println(c3 + " ");
      }
      
      rs.close();
      stmt.close();
      conn.close();
   }catch(SQLException se){
      
      se.printStackTrace();
   }catch(Exception e){
      
      e.printStackTrace();
   }finally{
      
      try{
         if(stmt!=null)
            stmt.close();
      }catch(SQLException se2){
      }
      try{
         if(conn!=null)
            conn.close();
      }catch(SQLException se){
         se.printStackTrace();
      }
   }
}
}

erro:

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

at java.net.URLClassLoader.findClass(Unknown Source)

at java.lang.ClassLoader.loadClass(Unknown Source)

at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)

at java.lang.ClassLoader.loadClass(Unknown Source)

at java.lang.Class.forName0(Native Method)

at java.lang.Class.forName(Unknown Source)

at Base.main(Base.java:16)

eu acho que e a falta do pom.xml ou alguma coisa relacionada as variáveis de sistema.
OBS:estou usando a ultima versão do MySQL.

1 Resposta

ClockWork
Solucao aceita

Arrumei as variáveis do sistema no classpatch e reiniciei o pc agora ta funcionando.

Criado 1 de novembro de 2017
Ultima resposta 2 de nov. de 2017
Respostas 1
Participantes 1