您的位置:首页 > 数据库

JDBC及简单抄作连接数据库源码

2007-07-23 08:29 423 查看
package com;

//import oracle.jdbc.driver.OracleDriver;
import java.sql.*;

public class Database {
private Connection conn = null;
private Statement stmt = null;
private ResultSet rs = null;
public Database() {
try {
// Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
// String url = "jdbc:oracle:thin:@localhost:1521:test"; //orcl为数据库的SID

Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url ="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=test"; //sqlserver为数据库的SID

// String user = "system";
// String password = "test";orcl为数据库的SID

String user = "sa";
String password = "";
conn = DriverManager.getConnection(url, user, password);
System.out.println("2000 is connected!");

}
catch (ClassNotFoundException ex) {
}
catch (IllegalAccessException ex) {
}
catch (InstantiationException ex) {
}
catch (SQLException e) {
e.printStackTrace();
}
;

}

//query
public ResultSet getQuery() {
String sql = "SELECT * FROM empl";
try {
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
return rs;
}
catch (SQLException ex) {
ex.printStackTrace();
// System.out.println(ex.getMessage());
return null;
}

}

//update
public void getUpdate() {
String sql = "update scott.dept set dname =" + "'hh'" + " where loc =" +
"'DALLAS'";
System.out.println(sql);
try {
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
int i = stmt.executeUpdate(sql);
if (i > 0) {
System.out.println("update success!");
}
else {
System.out.println("update is failure!");
}

}
catch (SQLException e) {

}
}

//insert
public void getInsert() {
String sql = "insert into empl(name,sex) values(" + "'guo'," + "'nan')";
System.out.println(sql);
try {
stmt = conn.createStatement();
int i = stmt.executeUpdate(sql);
if (i > 0) {
System.out.println("update success!");
}
else {
System.out.println("update is failure!");
}

}
catch (SQLException e) {

}
}

//delete
public void getDel() {
String sql = "delete from empl where id =2";
try {
stmt = conn.createStatement();
int i = stmt.executeUpdate(sql);
if (i > 0) {
System.out.println("update success!");
}
else {
System.out.println("update is failure!");
}
}
catch (SQLException e) {
System.out.println(e.getMessage());
}
}

public void closeDb() {
try {
rs.close();
stmt.close();
conn.close();
System.out.println("the DB connection is shut down!");
}
catch (Exception ex) {
ex.printStackTrace();
// System.out.println(ex.getMessage());
}
finally {
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
}
catch (Exception ex) {
System.out.println("关不掉数据库了 :(");
}
}
}

public static void main(String[] args) {
Database db = new Database();
ResultSet rs = db.getQuery();
try {
// db.getDel();
while (rs.next()) {
System.out.print(rs.getInt(1) + "/t");
System.out.print(rs.getString("name") + "/t");
System.out.print(rs.getString("sex") + "/t");
System.out.println();
}

db.closeDb();
}
catch (Exception e) {
e.printStackTrace();
// System.out.println("我晕了 --||");
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: