Access to | data | The database must first be loaded to drive. Don't like me, toss for hours of MySQL visit. Later found to have made a most basic and most fatal error-no load driver! Think about all the sweat color.
Several common ways to access data are as follows:
Sql server:
Url:
Jdbc:microsoft:sqlserver://localhost:1433;databasename=pubs
Driver:com.microsoft.jdbc.sqlserver.SQLServerDriver
Oracle:
Url:
Jdbc:oracle:oci8: @newer
Jdbc:oracle:thin:@10.0.0.200:1521:newer (??: Jdbc:oracle:thin:@[ip]:[port]:[sid])
Driver:oracle.jdbc.driver.OracleDriver
Mysql
Url:
Jdbc:mysql://127.0.0.1:3306/mysql
Driver:org.gjt.mm.mysql.Driver
It is easy to observe that the format is the same. Note that only three of the school's parameters are listed here, with a specific application in the following example.
The latest Mm.mysql drive can be directly www.mysql.org up and down, as if MySQL had Mm.mysql incorporated into the official drive. Download the JDBC driver on the www.mysql.org.
To give an example of implementation, not I wrote, but feel good, representative.
/*
* Created on 2005-7-15
*
* TODO to change the template of this generated file go
* Window-preferences-java-code Style-code Templates
*/
Package Com.phzhong;
/**
* @author Administrator
*
* TODO to change the template of this generated type comment go
* Window-preferences-java-code Style-code Templates
*/
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import java.sql.Statement;
public class Dbmanager {
User name
Private String user = "";
Password
Private String Password = "";
Host
Private String host = "";
Database name
Private String Database = "";
/*
Private String
Url= "jdbc:mysql://" +host+ "/" + "useunicode=true&characterencoding=gb2312";
*/
Private String URL = "";
private Connection con = null;
Statement stmt;
/**
* Get a connection based on the host, database name, database user name, database user password.
* @param host String
* @param database String
* @param user String
* @param password String
*/
Public Dbmanager (string host, string database, string user, string password) {
This.host = host;
This.database = database;
This.user = user;
This.password = password;
Show Chinese
This.url = "jdbc:mysql://" + host + "/" + Database + "? useunicode=true&characterencoding=gb2312";
try {
Class.forName ("Org.gjt.mm.mysql.Driver");
}
catch (ClassNotFoundException e) {
SYSTEM.ERR.PRINTLN ("Class not found:" + e.getmessage ());
}
try {
con = drivermanager.getconnection (This.url, This.user, This.password);
The connection type is resultset.type_scroll_insensitive, resultset.concur_read_only
stmt = Con.createstatement (resultset.type_scroll_insensitive,resultset.concur_read_only);
}
catch (SQLException a) {
SYSTEM.ERR.PRINTLN ("SQL exception:" + A.getmessage ());
}
}
/**
* Returns the connection made
*/
Public Connection Getcon () {
return con;
}
/**
* Execute a simple query statement
* Returns the result set obtained
*/
Public ResultSet executequery (String sql) {
ResultSet rs = null;
try {
rs = stmt.executequery (SQL);
}
catch (SQLException e) {
E.printstacktrace ();
}
Return RS;
}
/**
* Execute a simple UPDATE statement
* Returns true for successful execution
*/
public boolean executeupdate (String sql) {
Boolean v = false;
try {
v = stmt.executeupdate (sql) > 0? True:false;
}
catch (SQLException e) {
E.printstacktrace ();
}
finally {
return v;
}
}
public static void Main (string[] args) {
ResultSet rs;
Dbmanager exe = new Dbmanager ("192.168.0.222", "Test", "root", "111");
rs = Exe.executequery ("SELECT * from Encodingtest");
try{
while (Rs.next ()) {
System.out.println (Rs.getint ("Sid") + "" + rs.getstring ("str"));
}
}catch (Exception e) {
}
}
}
Code can run.