经过两个月的学习现在总对Jbuilder 2007有点感觉了,学习Java时开始,我选择的是Jbuilder 2005。后发现出了新的了。叫2007下来一下。妈呀!全不一样。。我重学呀!不过还好。他功能强呀。。。不比。NET差。
我用了好久才学会使用数据库。现将代码贴如下:
package news;
//sql_data.java
import java.sql.*;
public class sql_data {
String sDBDriver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String url ="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=InfoSite";
//pubs为你的数据库的
String user = "sa";
String password = "sa";
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
public sql_data() {
try {
Class.forName(sDBDriver);
} catch (java.lang.ClassNotFoundException e) {
System.err.println("sql_data(): " + e.getMessage());
}
}
public void executeInsert(String sql) {
try {
//conn = DriverManager.getConnection(url,user,password);
conn = DriverManager.getConnection(url, user, password);
stmt = conn.createStatement();
stmt.executeUpdate(sql);
stmt.close();
conn.close();
} catch (SQLException ex) {
System.err.println("sql_data.executeUpdate:" + ex.getMessage());
}
}
public ResultSet executeQuery(String sql) {
try {
conn = DriverManager.getConnection(url, user, password);
stmt =
conn.createStatement(
ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(sql);
} catch (SQLException ex) {
System.err.println("sql_data.executeQuery:" + ex.getMessage());
}
return rs;
}
public void executeUpdate(String sql) {
try {
conn = DriverManager.getConnection(url, user, password);
stmt = conn.createStatement();
stmt.executeUpdate(sql);
stmt.close();
conn.close();
} catch (SQLException ex) {
System.err.println("aq.executeQuery: " + ex.getMessage());
}
}
public void executeDelete(String sql) {
try {
conn = DriverManager.getConnection(url, user, password);
stmt = conn.createStatement();
stmt.executeUpdate(sql);
stmt.close();
conn.close();
} catch (SQLException ex) {
System.err.println("sql_data.executeDelete:" + ex.getMessage());
}
}
public void closeStmt() {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void closeConn() {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
/*
*
用于测试数据库是否通
public static void main(String avg[]){
sql_data db=new sql_data();
String sql="select* from admin";
ResultSet rs=db.executeQuery(sql);
try {
while (rs.next()){
System.out.print(rs.getString("adminuser"));
System.out.println(rs.getString("adminpass"));
}
} catch (Exception e) {
// TODO: handle exception
}
}
*/
}