当前位置: 技术问答>java相关
jdbc的问题,急!!!!!
来源: 互联网 发布时间:2017-04-06
本文导语: 我想写两个类,一个用来连接数据库,一个用来对数据库进行操作,可不可以,应怎么做,能给个简单的例子嘛? | class connetionJDBC{//建立连接类 Connection con; public Connection getConnection(){ ...
我想写两个类,一个用来连接数据库,一个用来对数据库进行操作,可不可以,应怎么做,能给个简单的例子嘛?
|
class connetionJDBC{//建立连接类
Connection con;
public Connection getConnection(){
try {
Class.forName ("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection("jdbc:oracle:thin:@url", "user", "password");
return con;
} catch(Exception clsex) {
return null;
}
}
}
class userJDBC{//对数据库操作类
connetionJDBC test;
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql="select....";
userJDBC(){
try {
con = test.getConnection();
pstmt = conn.prepareStatement("sql");
rs = pstmt.executeQuery();
while(rs.next()) {
listFormhashtable.put(rs.getString("id"),rs.getString("name"));
}
} catch(Exception clsex) {
}
}
}
|
package bbs;
import java.sql.*;
public class cn
{
String cline="com.microsoft.jdbc.sqlserver.SQLServerDriver";
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=xiruo";
String user="sa";
String pass="";
Connection conn=null;
ResultSet rs=null;
public cn() {
try {
Class.forName(cline);
}
catch(java.lang.ClassNotFoundException e)
{
System.err.println("classnotfound:"+e.getMessage());
}
}
public ResultSet executeQuery(String sql) {
rs=null;
try {
conn=DriverManager.getConnection(url,user,pass);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs=stmt.executeQuery(sql);
}
catch(SQLException e) {
System.err.println("SQL error:"+e.getMessage()+"rn"+sql);
}
return rs;
}
public void executeUpdate(String sql) {
try {
conn=DriverManager.getConnection(url,user,pass);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
stmt.executeUpdate(sql);
}
catch(SQLException e) {
System.err.println("SQL error:"+e.getMessage()+"rn"+sql);
}
}
}
import java.sql.*;
public class cn
{
String cline="com.microsoft.jdbc.sqlserver.SQLServerDriver";
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=xiruo";
String user="sa";
String pass="";
Connection conn=null;
ResultSet rs=null;
public cn() {
try {
Class.forName(cline);
}
catch(java.lang.ClassNotFoundException e)
{
System.err.println("classnotfound:"+e.getMessage());
}
}
public ResultSet executeQuery(String sql) {
rs=null;
try {
conn=DriverManager.getConnection(url,user,pass);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs=stmt.executeQuery(sql);
}
catch(SQLException e) {
System.err.println("SQL error:"+e.getMessage()+"rn"+sql);
}
return rs;
}
public void executeUpdate(String sql) {
try {
conn=DriverManager.getConnection(url,user,pass);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
stmt.executeUpdate(sql);
}
catch(SQLException e) {
System.err.println("SQL error:"+e.getMessage()+"rn"+sql);
}
}
}
|
try {
Class.forName ("oracle.jdbc.driver.OracleDriver");
connection = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:G", "G", "G");
} catch(ClassNotFoundException clsex) {
}
catch(SQLException sqlex) {
}
Class.forName ("oracle.jdbc.driver.OracleDriver");
connection = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:G", "G", "G");
} catch(ClassNotFoundException clsex) {
}
catch(SQLException sqlex) {
}
|
good enough!