几种常见的数据访问方式如下:
sqlserver:
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
观察一下很容易发现格式是一样的。注意,这里只列出来学校的三个参数,后面例子中有个具体的应用。
最新的 的mm.mysql驱动可以直接再www.mysql.org上下载,好像是mysql把mm.mysql收编到了官方驱动中了。下载www.mysql.org上的jdbc驱动就可以了。
给个实现的例子,不是我写的,不过觉得不错,有代表性。
/*
* created on 2005-7-15
*
* todo to change the template for this generated file go to
* window - preferences - java - code style - code templates
*/
package com.phzhong;
/**
* @author administrator
*
* todo to change the template for this generated type comment go to
* 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 {
//用户名
private string user = "";
//密码
private string password = "";
//主机
private string host = "";
//数据库名字
private string database = "";
/*
private string
url="jdbc:mysql://"+host+"/"+"useunicode=true&characterencoding=gb2312";
*/
private string url ="";
private connection con = null;
statement stmt;
/**
* 根据主机、数据库名称、数据库用户名、数据库用户密码取得连接。
* @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;
//显示中文
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);
//连接类型为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());
}
}
/**
* 返回取得的连接
*/
public connection getcon() {
return con;
}
/**
* 执行一条简单的查询语句
* 返回取得的结果集
*/
public resultset executequery(string sql) {
resultset rs = null;
try {
rs = stmt.executequery(sql);
}
catch (sqlexception e) {
e.printstacktrace();
}
return rs;
}
/**
* 执行一条简单的更新语句
* 执行成功则返回true
*/
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){
}
}
}
代码可以运行。
文章整理:西部数码--专业提供域名注册、虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!


