[Sharing a small error in the JRE setting in]eclipse 3.0 causes connection to the database in the Java file failed

Source: Internet
Author: User
Tags sql mysql string trim
Error | Connecting to database Java.lang.ClassNotFoundException:org.gjt.mm.mysq.Driver
This is probably the most common mistake, the general solution is to check whether the database driver is installed in the JDK directory

(such as: Copy the Mysql-connector-1-3-1.jar file to the Lib folder in the JDK directory)

Classpath whether a driver directory is set

(classpath=.;D: \ J2sdk\lib\dt.jar;d:\j2sdk\lib\tools.jar;d:\j2sdk\lib\mysql-connector-1-3-1.jar;).

Both of my settings are correct, there is no problem testing the connection code, but in eclipse it is an error (as above)

Import java.sql.*;

public class Mysqltest {

public static void Main (string[] args) {//drivername String drivername= "Org.gjt.mm.mysql.Driver";  The database username String username= "root";  PSW String userpasswd= "SS";  dbname String dbname= "Java_app";  TableName String tablename= "Staff";    Conncet comments String url= "jdbc:mysql://localhost/" +dbname+ "user=" +username+ "&password=" +USERPASSWD;     System.out.println (URL); try{Class.forName (drivername). newinstance ();

   Connection connection=drivermanager.getconnection (URL);   Statement Statement = Connection.createstatement ();   String sql= "SELECT * from" +tablename;   ResultSet rs = Statement.executequery (SQL);//    get result   resultsetmetadata Rmeta = Rs.getMetaData (); /    get col   int numcolumns=rmeta.getcolumncount ();//    output    for (int i=1;i<=numcolumns;i++) {       if (i<numcolumns)           System.out.print (Rmeta.getcolumnname (i) + "| ");       else         System.out.println ( Rmeta.getcolumnname (i));      }       while (rs.next ()) {          for (int i=1;i<=numcolumns;i++) {               if (i<numcolumns)                   System.out.print (rs.getstring (i). Trim () + "| ");              else                  System.out.println (rs.getstring (i). Trim ());         }      }     Rs.close ();    connection.close ();  }catch (Exception ex) {      System.out.println (ex);      system.exit (0);  }

}}

After confirming that there are no problems with the JDK, I turn my attention to eclipse settings.

(eclipse-->window-->preferences-->java-->installed JREs)



Edit



The little mistake is right here! Cancel use Default system libraries



Add the jar package where the database driver resides



Ok! Save Settings restart Eclipse, run test program successful!


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.