java中访问数据库操作
几种常见的数据访问方式如下:
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){
}
}
}
代码可以运行。