Summary of JDBC read/write mysql

Source: Internet
Author: User
1. Download the driver (mainly jar package) mysql-connection-java-5.1.31-bin.jar 2. Add the jar package, configure the environment variables to add the driver jar package to the following two directories, and set the classpath environment variables. 1. Add the Directory: (CATALINA_HOME) D:... Tomcat6.0lib (JAVA_HOME) D:... jdk1.7jrelib2.

1. Download the driver (mainly jar package) mysql-connection-java-5.1.31-bin.jar 2. Add the jar package, configure the environment variables to add the driver jar package to the following two directories, and set the classpath environment variables. 1. Add the Directory: (CATALINA_HOME) D: \ .. \ Tomcat 6.0 \ lib \ (JAVA_HOME) D: \ .. \ jdk1.7 \ jre \ lib \ 2.

1. Download the driver (mainly jar package)

Mysql-connection-java-5.1.31-bin.jar

2. Add a jar package and configure Environment Variables

Add the driver jar package to the following two directories and set the classpath environment variable.

1. Add a directory:

(CATALINA_HOME) D: \ .. \ Tomcat 6.0 \ lib \

(JAVA_HOME) D: \ .. \ jdk1.7 \ jre \ lib \

2. Add the environment variable: classpath

----- Remember to set the environment variable after adding the jar package !!! The same is true for MySQLdb in python. environment variables must be set.

D: \ mysql-connector-java-5.1.31 \ mysql-connector-java-5.1.31-bin.jar;

% JAVA_HOME % \ jre \ lib \ mysql-connector-java-5.1.31-bin.jar;

% CATALINA_HOME % \ lib \ mysql-connector-java-5.1.31-bin.jar;

3. Set the mysql driver in eclipse

In eclipse's "Preferences", select JDBC Driver in SQL Explorer and edit the MySQL Driver on the right.

Extra Class Path tab, Add button, Add Driver jar package mysql-connection-java-5.1.31-bin.jar, and set DriverClass Name to com. mysql. jdbc. Driver.

4. Import external JAR into the project build path as the driver jar package

(Then you can write the jdbc link to the database program. For details about the program, if you want to write a. SQL script directly, you still need step 2)

5. SQL scripts directly link to the database

Open perspective SQL Explorer, right-click the Connection window, and choose Create New Connection Profile,

Name, such as testyy;

The Driver is MySQL Driver;

The URL is jdbc: mysql: // localhost: 3306/testdb ------- (your own database name is testdb)

The User Name is root ------Tens of millionsRoot!!! When installing mysql, select "default" as the configuration. The user will always be root, but the password is changed.

Password is the mysql Password;

Check Auto Logon and click OK;

Right-click the Connection link, create a New SQL Editor, and edit the. SQL Script: select * from table;

Click the "Runner" button in the upper left corner. The output below is the same as that in the mysql command line.

-------------

Separate jdbcDemo. java program:

Package com. hadoop. mysql; import java. SQL. connection; import java. SQL. driverManager; import java. SQL. resultSet; import java. SQL. SQLException; import java. SQL. statement;/*** @ Note: "add build path" is required when connecting to the database. Add the jar package: for jdbc driver ---- "mysql-connector-java-5.1.31-bin.jar" * Note that the database name is testdb (written in url), the table name is student (written in selete * from student in SQL) **/public class MyJdbcDemo {public static void main (String [] args) {String driver = "Com. mysql. jdbc. driver "; // Driver DriverString url =" jdbc: mysql: // localhost: 3306/testdb "; // url. The database name is testdbString user =" root "; // username ------ note that the user name must be root !!! The username can only be rootString password = "mysql" by default; // pwd ----- the password can be mysql, and try {Class set by yourself. forName (driver); // load the driver Connection conn = DriverManager. getConnection (url, user, password); // create a connection if (! Conn. isClosed () {System. out. println ("Succeed connecting to the database! ");} Statement statement = conn. createStatement (); // execute the SQL statementString SQL = "select * from student"; // SQL statement. The table name is studentResultSet result = statement.exe cuteQuery (SQL ); // result set resultSystem. out. println ("execution result:"); System. out. println ("sno" + "\ t" + "sname" + "\ t" + "sex" + "\ t" + "bdate" + "\ t" + "height "); while (result. next () {System. out. print (result. getString ("sno") + "\ t"); System. out. print (result. getString ("sname ") + "\ T"); // select the sname column data System. out. print (result. getString ("sex") + "\ t"); System. out. print (result. getString ("bdate") + "\ t"); System. out. println (result. getString ("height");} result. close (); conn. close ();} catch (ClassNotFoundException e) {System. out. println ("cannot find the driver! ");} Catch (SQLException e) {e. printStackTrace (); System. out. println (" SQLException! ");}}}

Results In the console:


The results shown in the database table (the mysql terminal is not opened here, and the results are also displayed in SQL Explorer ):

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.