JDBC (Java Data Base Connectivity, java database connection) is a Java API for executing SQL statements. It can provide unified access to multiple relational databases. It consists of a set of classes and interfaces written in the Java language. JDBC provides a benchmark that allows more advanced tools and interfaces to enable database developers to write database applications.
If you want to use a database, you need to add a database driver. Different databases have drivers that are not used. I will not explain them one by one here. The method of adding a jar program driver package will not be explained here.
Another article contains an introduction //www.VeVB.COM/article/47945.htm
The following is an example to introduce the connection to the mysql database. The methods of other databases are similar.
import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Connection;import java.sql.Statement;pu blic class MysqlDemo { public static void main(String[] args) throws Exception { Connection conn = null; String sql; // MySQL's JDBC URL writing method: jdbc:mysql://host name: connection port/database name? Parameters = value// To avoid Chinese garbled, useUnicode and charac are required terEncoding // Before executing database operations, you need to create a database on the database management system, and the name is determined by yourself. // Before the following statement, you need to create a javademo database String url = "jdbc:mysql://localhost:3306/javademo?" + "user= root&password=root&useUnicode=true&characterEncoding=UTF8"; try { // The reason for using the following statement is that we need to use MySQL driver, so we need to drive it, // You can use Class.forName to drive it Load in, It can also be driven through initialization. The following three forms can be Class.forName("com.mysql.jdbc.Driver");// Dynamically load mysql driver// or:// or:// com.mysql.jdbc.Driver driver = new com.mysql.jdbc.Driver(); // or: // new com.mysql.jdbc.Driver(); System.out.println("MySQL driver loaded successfully"); // A Connection represents a database Connection conn = DriverManager.getConnection(url); // Statement contains many methods, such as executeUpdate, which can implement insertion, update and delete, etc. Statement stmt = conn.createStatement(); sql = "create tab le student(NO char(20) ,name varchar(20),primary key(NO))"; int result = stmt.executeUpdate(sql);// The executeUpdate statement will return an affected number of rows. If -1 is returned, it will not succeed if (result != -1) { System.out.println("Create the data table successfully"); sql = "insert into student(NO,name) values('2012001','Tao Weiji')"; 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 will return the result set, otherwise it will return the empty value System.out.println("Student number/t name"); while (rs.next()) { System.out .println(rs.getString(1) + "/t" + rs.getString(2));// Enter If the returned int type, you can use getInt() } } } catch (SQLException e) { System.out.println("MySQL operation error"); e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { conn.close(); } }}
Also attach a method of JDBC connecting 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 is the SIDString of Oracle database user="test"; String password="test";Connection con=DriverManager.getConnection(url,user,password);
2) Connect to 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) Connect to MySQL database
Class.forName("com.mysql.jdbc.Driver");String url="JDBC:mysql://localhost:8080/testDB";String user="test"; String password="test";C onnection con=DriverManager .getConnection(url,user,password);
4) Connect to SQL Server2000 database
Class.forName("com.microsoft.JDBC.sqlserver.SQLServerDriver");String url="JDBC:microsoft:sqlserver://localhost:1433;DatabaseName=testD b";String user="test"; String password="test ";Connection con=DriverManager.getConnection(url,user,password);
5) Connect to PostgreSQL database
Class.forName("org.postgresql.Driver");String url="JDBC:postgresql://localhost/testDb";String user="test"; String password="test"; Conne ction con=DriverManager.getConnection(url ,user,password);
6) Connect to the Access database
The code copy is as follows: Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="JDBC:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ="+application.getRealPath("/Data/testDb/mdb");
Connection conn=DriverManager.getConnection(url,"","");
7 Connect to 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 Connect to informix database
The code copy is as follows: Class.forName("com.informix.JDBC.ifxDriver");
String url="JDBC:informix-sqli:localhost:1533/testDb:INFORMIXSERVER=myserver"user=testUser;password=testpassword"; Connection con=DriverMan ager.getConnection(url);