日期:2014-05-16 浏览次数:20415 次
1 Oracle8/8i/9i(用thin模式)
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
string url="jdbc:oracle:thin;@localhost:1521:orcl";
?
2 SqlServer 2000
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
string url="jdbc:microsoft:sqlserver;//localhost:1433;DatabaseName=数据库名";
?
3 DB2
Class.forName("com.ibm.db2.jdbc.app.DB2Driver").newInstance();
string url="jdbc:db2;//localhost:5000/bookinfo";
?
4 MySql
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
string url = "jdbc:mysql://localhost/数据库名?user=用户名&password=密码&useUnicode=true&characterEncoding=8859_1";
?
?
?
jdbc:odbc桥方式
package com.ld.bak;
import java.sql.*;
public class UpDate {
??? public static void main(String[] args) {
??????? try {
???????????
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
??????????? Connection conn = DriverManager.getConnection("jdbc:odbc:student");
??????????? Statement stmt = conn.createStatement();
??????????? ResultSet rs1 = stmt.executeQuery("select * from student");
??????????? while (rs1.next()) {
??????????????? System.out.println(rs1.getString(1) + rs1.getString(2));
??????????? }
??????????? String sql1 = "update student set aploma_id='0' where aploma_id is null";
??????????? stmt.executeUpdate(sql1);
??????????? ResultSet rs2 = stmt.executeQuery("select * from student");
??????????? while (rs2.next()) {
??????????????? System.out.println(rs2.getString(1) + rs2.getString(2));
??????????? }
??????????? stmt.close();
??????????? conn.close();
??????? } catch (Exception e) {
??????????? System.out.println("Error!" + e);
??????? }
??? }
}
驱动方式
package com.ld.bak;
import java.sql.*;
public class ChineseQuery {
??? public static void main(String[] args) {
??????? // TODO Auto-generated method stub
??????? try {
??????????? String url = "jdbc:mysql://localhost:3306/land?useUnicode=true&characterEncoding=GB2312";
??????????? Class.forName("com.mysql.jdbc.Driver");
??????????? Connection conn = DriverManager.getConnection(url, "root",
??????????????????? "6573251");
??????????? Statement stmt = conn.createStatement();
??????????? ResultSet rs = stmt
??????????????????? .executeQuery("SELECT * FROM tbnews WHERE News_Id = BINARY '1'");
??????????? while (rs.next()) {
??????????????? System.out.println(rs.getInt(1) + "\t" + rs.getString(5));
??????????? }
??????? } catch (Exception e) {
??????????? e.printStackTrace();
??????? }
??? }
}