Use Java for MySQL Development (from: MySQL for Linux on power, Part 1: Application Development)

Source: Internet
Author: User
Tags apache tomcat

Use Java for MySQL Development

There is a very broad topic: Java-based multi-tier applications that communicate with MySQL and other relational databasesProgram. This section describes a simple example of using a local Java application to connect to MySQL.

To allow Java programs to communicate with specific databases, you need a Java database connectivity (JDBC) driver for that database. Like most mainstream relational database management systems (RDBMS), MySQL also has its own JDBC driver, which currently includes:

    • MySQL connector/J from MySQL AB
    • Resin JDBC driver

 

MySQL connector/J is the JDBC 3.0 API implementation for sun of MySQL RDBMS, and is the official JDBC driver for MySQL. It is written in Java, so it can run in any operating system with the appropriate JVM environment, including the Linux release version on power. Therefore, this example uses the connector/J driver. It is a type iv jdbc driver and is known to be applicable to multiple web application servers in Linux on power, such as IBM WebSphere, BEA WebLogic, Apache Tomcat, and JBoss.

Of course, in addition to the JDBC driver for MySQL, you also need the JDK itself. At the time of writing this article, IBM provided JDK 1.4.2, the latest version for Linux for power and PPC architectures, both 32-bit and 64-bit. You can obtain IBM developer kits for Java Technology Online (see references ).

Basic information displayed in this exampleCodeConnect to the MySQL database and run the query.

The complete Java code in this example is as follows:

Listing 2. Java sample code

Import Java. io. *; import Java. util. *; import Java. SQL. *; public class java_mysql {public static void display_rs (resultset RS) throws sqlexception {try {resultsetmetadata rsmd = Rs. getmetadata (); For (INT I = 1; I <= rsmd. getcolumncount (); ++ I) system. out. print ("\ t" + (rsmd. getcolumnname (I )). touppercase (); system. out. println (); While (RS. next () {for (Int J = 1; j <= rsmd. getcolumncount (); ++ J) {object OBJ = Rs. getObject (j); system. out. print ("\ t" + obj. tostring ();} system. out. println () ;}} catch (sqlexception e) {system. out. println ("sqlexception:" + E. getmessage (); system. out. println ("sqlstate:" + E. getsqlstate (); system. out. println ("vendorerror:" + E. geterrorcode (); E. printstacktrace (); system. exit (1) ;}} public static void main (string ARGs []) {statement statemen T = NULL; connection = NULL; resultset; string query, prompt, input; int choice =-1; try {class. forname ("com. mySQL. JDBC. driver "). newinstance ();} catch (exception e) {system. err. println ("unable to load driver. "); E. printstacktrace (); system. exit (1);} Try {string url = "JDBC: mysql: // localhost/contracting"; string username = "username"; string Password = "password"; connection = driv Ermanager. getconnection (URL, username, password);} catch (sqlexception e) {system. out. println ("sqlexception:" + E. getmessage (); system. out. println ("sqlstate:" + E. getsqlstate (); system. out. println ("vendorerror:" + E. geterrorcode (); E. printstacktrace (); connection = NULL; system. exit (1 );} prompt = "\ n \ t \ T1. show contents of the table job \ n" + "\ t \ T2. exit \ n"; while (true) {system. out. P Rintln (prompt); try {bufferedreader in = new bufferedreader (New inputstreamreader (system. in); input = in. readline (); choice = integer. parseint (input);} catch (exception e) {e. printstacktrace (); system. exit (1);} Try {Switch (choice) {Case 1: query = "select * from job;"; Statement = connection. createstatement (); resultset = statement.exe cutequery (query); display_rs (resultset); break; Case 2: S Ystem. Out. println ("Bye! "); System. Exit (0); default: system. Err. println (" invalid value entered! ") ;}} Catch (sqlexception e) {system. out. println ("sqlexception:" + E. getmessage (); system. out. println ("sqlstate:" + E. getsqlstate (); system. out. println ("vendorerror:" + E. geterrorcode (); E. printstacktrace (); connection = NULL; system. exit (1 );}}}}

FunctionDisplay_rs ()Shown inResultsetStandard Code of the object. Connection to the MySQL databaseMain ()Function.

Implementation in MySQL connector/JJava. SQL. DriverThe Class Name of isCom. MySQL. JDBC. Driver.Org. gjt. Mm. MySQL. DriverThe class name can be used to maintain backward compatibility with older versions.

The most common way for JVM to use the connector/j jdbc driver isClasspathThe variable contains the path of the mysql-connector-Java-[version]-bin. jar file.

The following is a string followed by the common jdbc url format:

String url = "JDBC: mysql: // localhost/contracting ";

After successfully connecting to the MySQL server and database, you can perform a simple query as follows:

1. Show contents of the table job 2. Exit

Option "1" will display the query result of "select * from job.

Job_code job_name job_hour_chrg 200 application programmer 35.48 201 Database Administrator 38.50 202 technical support 27.00 207 Database Designer 49.99

The second option will generate the content of the job table, and the third option will close the program.

This example uses the 64-bit JDK 1.4.2 provided by IBM for Linux for power and PPC architectures.

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.