不多说,直接上源码,可以直接复用哦!
import java.sql.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*
/**
*
* @author cqx
*/
public class DbHelper {
static String driver = "org.apache.derby.jdbc.EmbeddedDriver" ;
static String dbName = "selectclass_db";
static String dbURL = "jdbc:derby:" + dbName + ";create=true" ;
static Connection conn=null;
static Statement st;
public static void getConnection(){
try {
Class.forName(driver);
conn = DriverManager.getConnection(dbURL);
}catch(Exception e){
e.printStackTrace();
}
}
public static boolean ExecuteSql(String sql){
getConnection();
try{
st=conn.createStatement();
int r=st.executeUpdate(sql);
if(r>0){
return true;
}else{
return false;
}
}catch(Exception e){
e.printStackTrace();
try{
conn.close();
}catch(Exception ex){
ex.printStackTrace();
}
return false;
}
}
public static boolean SelectSql(String sql){
getConnection();
boolean flag=false;
try{
st=conn.createStatement();
ResultSet rs=st.executeQuery(sql);
while(rs.next()){
flag=true;
break;
}
}catch(Exception e){
e.printStackTrace();
try{
conn.close();
}catch(Exception ex){
ex.printStackTrace();
}
}
return flag;
}
public static ResultSet QuerySql(String sql){
getConnection();
ResultSet rs=null;
try{
st=conn.createStatement();
rs=st.executeQuery(sql);
}catch(Exception e){
e.printStackTrace();
try{
conn.close();
}catch(Exception ex){
ex.printStackTrace();
}
}
return rs;
}
}