Java connects MySQL database via JDBC _java

Source: Internet
Author: User
Tags db2 informix odbc postgresql stmt sybase sybase database

JDBC (Java Data Base Connectivity,java database connection) is a Java API for executing SQL statements that provides unified access to a variety of relational databases, consisting of a set of classes and interfaces written in the Java language. JDBC provides a benchmark by which you can build more advanced tools and interfaces that enable database developers to write database applications.

If you want to use the database to add a database driver, different databases have no driver, here is not one of the ways to add a Jar program driver package is not explained here,

Another article contains an introduction to Http://www.jb51.net/article/47945.htm

The following is an example to introduce the MySQL database connection, the other database methods are similar.

Import Java.sql.DriverManager;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import java.sql.Connection;
Import java.sql.Statement;
    public class Mysqldemo {public static void main (string[] args) throws Exception {Connection conn = null;
    String SQL; MySQL jdbc URL write way: jdbc:mysql://host Name: Connection port/database name? parameter = value//Avoid Chinese garbled to specify Useunicode and characterencoding//Perform database operations before data
        Library Management system to create a database, the first name set,///The following statement must first create the Javademo database String url = "Jdbc:mysql://localhost:3306/javademo?"
    + "User=root&password=root&useunicode=true&characterencoding=utf8"; try {//The following statement is used to use the MySQL driver, so we have to drive it,//can be loaded through the class.forname, can also be initialized to drive, the following three forms can be Cla Ss.forname ("Com.mysql.jdbc.Driver")//dynamically load MySQL driver//or://com.mysql.jdbc.Driver Driver = new COM.MYSQL.JDBC .
      Driver ();
      OR://new Com.mysql.jdbc.Driver ();
      System.out.println ("Successfully loaded MySQL driver"); A connection represents a database connection conn = DrivermaNager.getconnection (URL);
      Statement contains many methods, such as executeupdate can be inserted, updated and deleted Statement stmt = Conn.createstatement ();
      sql = "CREATE TABLE student (no char (), name varchar (), primary key (No))"; int result = stmt.executeupdate (SQL);//Executeupdate statement returns an affected number of rows, if return-1 There is no success if (Result!=-1) {System.ou
        T.PRINTLN ("Create data table succeeded");
        sql = "INSERT into student (No,name) VALUES (' 2012001 ', ' Tao Weiki ')";
        result = Stmt.executeupdate (SQL);
        sql = "INSERT into student (No,name) VALUES (' 2012002 ', ' Zhou Xiaojun ')";
        result = Stmt.executeupdate (SQL);
        sql = "SELECT * from student";
        ResultSet rs = stmt.executequery (sql);//ExecuteQuery Returns a collection of results, otherwise null System.out.println ("number \ t name"); while (Rs.next ()) {System.out. println (rs.getstring (1) + "\ T" + rs.getstring (2));/Enter if the type of int returned can be
      To catch (SQLException e) {System.out.println ("MySQL operation Error") with Getint ()}}};
    E.printstacktrace (); } catch (Exception e) {e.printstacktrace ();
    finally {conn.close ();

 }
  }
}

Another way to attach JDBC connection to various databases (classic)

1) Connect Oracle 8/8i/9i/10g/11g (thin mode)

Class.forName ("Oracle.") JDBC.driver.OracleDriver "). newinstance ();

String Url= "JDBC:oracle:thin: @localhost: 1521:orcl"    //orcl a SID

string user= "test" for an Oracle database;

String password= "Test";

Connection con=drivermanager.getconnection (Url,user,password);

2) Connect DB2 database

Class.forName ("Com.ibm.db2.jcc.DB2Driver");

String url= "Jdbc:db2://localhost:5000/testdb";

String user= "Test"; String password= "Test";

Connection con=drivermanager.getconnection (Url,user,password);

3) connection MySQL database

Class.forName ("Com.mysql.jdbc.Driver");

String url= "Jdbc:mysql://localhost:8080/testdb";

String user= "Test"; String password= "Test";

Connection con=drivermanager.getconnection (Url,user,password);

4) Connect SQL Server2000 Database

Class.forName ("Com.microsoft.JDBC.sqlserver.SQLServerDriver");

String url= "Jdbc:microsoft:sqlserver://localhost:1433;databasename=testdb";

String user= "Test"; String password= "Test";

Connection con=drivermanager.getconnection (Url,user,password);

5) Connect PostgreSQL database

Class.forName ("Org.postgresql.Driver");

String url= "Jdbc:postgresql://localhost/testdb";

String user= "Test"; String password= "Test";

Connection con=drivermanager.getconnection (Url,user,password);

6) Connect to an Access database

Copy Code code as follows:
Class.forName ("Sun.jdbc.odbc.JdbcOdbcDriver");

String url= "Jdbc:odbc:driver={microsoft Access Driver (*.mdb)};D bq=" +application.getrealpath ("/data/testdb/mdb");

Connection conn=drivermanager.getconnection (URL, "", "");

7 Connecting the Sybase database

Class.forName ("Com.sybase.JDBC.SybDriver");

String url= "Jdbc:sybase:tds:localhost:5007/testdb";

Properties pro=system.getproperties ();

Pro.put ("User", "userId");

Pro.put ("Password", "User_password");

Connection con=drivermanager.getconnection (Url,pro);

8 Connecting the Informix database

Copy Code code as follows:
Class.forName ("Com.informix.JDBC.ifxDriver");

String url= "Jdbc:informix-sqli:localhost:1533/testdb:informixserver=myserver" user=testuser;password= Testpassword "; Connection con=drivermanager.getconnection (URL);

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.