单机的话用JDBCODBC桥吧比较方便
/*以下用JdbcOdbc桥连接举例*/
/*这个类用来连接数据库*/
class GetConnect {
public Connection getCon() {
static Connection con = null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (ClassNotFoundException ce) {
System.out.println(ce);
}
try {
con = DriverManager.getConnection("jdbc:odbc:数据源名");
}
return con;
}
}
/*这个类用来测试*/
class Test {
public static void main(String[] args) {
try{
Connection con = GetConnect.getCon(); //获得连接
Statement s = con.createStatement();
ResultSet rs = s.executeQuery("select * from StuInfo"); //执行SQL语句,返回ResultSet对象
while (rs.next()) {
System.out.print(rs.getString(1)); //读取ResultSet中的值
}
} catch (SQLException e) {
System.out.println(e);
}
}
}
[此贴子已经被作者于2006-8-2 15:38:51编辑过]
package deng;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class shujuku {
private Properties getProperties(){
Properties properties = new Properties();
try {
properties.load(shujuku.class.getResourceAsStream("dwt.properties"));
} catch (IOException ex) {
ex.printStackTrace();
}
return properties;
}
public String getUserName(){
String userName = getProperties().getProperty("database.user");
System.out.println(userName);
return userName;
}
public String getPassword(){
String password = getProperties().getProperty("database.password");
System.out.println(password);
return password;
}
public String getUrl(){
String url = getProperties().getProperty("database.url");
System.out.println(url);
return url;
}
public Connection getConnection() {
Connection conn = null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection(getUrl(),getUserName(),getPassword());
} catch (ClassNotFoundException ex) {
System.out.println("发生ClassNotFoundException异常!");
}catch(SQLException ex){
System.out.println("发生SQLException异常!");
}
return conn;
}
public void queryStudent(){
try {
Statement st = getConnection().createStatement();
ResultSet rst = st.executeQuery("SELECT * FROM jobs");
while(rst.next()){
System.out.println(rst.getString(1));
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
public static void main(String[] args){
shujuku stdo = new shujuku();
stdo.getUserName();
stdo.queryStudent();
}
}
这个例子是数据库连接的示例,可以拿去参考一下!
package 苦中乐;
import java.sql.*;
public class ChaXunTest {
/**
* mysql4.01与MyEclipse 用的mysql的驱动程序
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Connection con=null;
Statement st;
ResultSet rs=null;
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql" +
"?useUnicode=true&characterEncoding=gb2312","","");
st=con.createStatement();
rs=st.executeQuery("select * from student");
boolean flag=false;
while(rs.next()){
int id=rs.getInt("ID");
String name=rs.getString("name");
String sex=rs.getString("sex");
int age=rs.getInt("age");
String classJi=rs.getString("class");
String depart=rs.getString("depart");
System.out.print("ID:"+id+"\t"+"name:"+name+"\t"+"sex:"+sex+"\t"+
"age:"+age+"\t"+"class:"+classJi+"\t"+"depart:"+depart+"\n");
}
rs.close();
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
[此贴子已经被作者于2006-9-4 19:38:19编辑过]