作者在 2011-12-10 10:26:04 发布以下内容
class JDBCConn{
private static Connection con;
private PreparedStatement pstm;
private ResultSet rs;
/**
*1.加载驱动程序
*2.获得连接
*/
private JDBCConn(){
try{
Class.forName(String url);
System.out.println("加载驱动程序成功");
con= DriverManager.getConnection(String driverName,String userName,String userPass);
}catch(ClassNotFoundException e){
System.out.println("加载驱动程序异常,没有找到驱动程序");
}catch(SQLException e){
System.out.println("获取连接失败");
}
}
public static Connection getConnection(){
return con;
}
/**
*sql ="insert into table values(?,?,?,?)";
*/
public static String getExecute(String sql,String value){
String flag ;
try{
this.getConnection();
con.preparedStatement(sql);
flag = con.executeUpdate();
}catch(Exception e){
System.out.println(e.getMessage());
}
if(flag==null){
flag ="没有经过数据处理";
}
return flag;
}
/*
*public static String getExecute(String sql,Object obj){
*}
*/
private JDBCConn jdbcConn;
/**
*jdbc单例模式
*/
public static JDBCConn initInstance(){
if(jdbcConn==null){
jdbcConn = new JDBCConn();
}
return jdbcConn;
}
}
public class JDBCConnDemo{
public static void main(String args[]){
JDBCConn jdbcConn = this.initInstance();
this.getExecute(sql,"value");
}
}
private static Connection con;
private PreparedStatement pstm;
private ResultSet rs;
/**
*1.加载驱动程序
*2.获得连接
*/
private JDBCConn(){
try{
Class.forName(String url);
System.out.println("加载驱动程序成功");
con= DriverManager.getConnection(String driverName,String userName,String userPass);
}catch(ClassNotFoundException e){
System.out.println("加载驱动程序异常,没有找到驱动程序");
}catch(SQLException e){
System.out.println("获取连接失败");
}
}
public static Connection getConnection(){
return con;
}
/**
*sql ="insert into table values(?,?,?,?)";
*/
public static String getExecute(String sql,String value){
String flag ;
try{
this.getConnection();
con.preparedStatement(sql);
flag = con.executeUpdate();
}catch(Exception e){
System.out.println(e.getMessage());
}
if(flag==null){
flag ="没有经过数据处理";
}
return flag;
}
/*
*public static String getExecute(String sql,Object obj){
*}
*/
private JDBCConn jdbcConn;
/**
*jdbc单例模式
*/
public static JDBCConn initInstance(){
if(jdbcConn==null){
jdbcConn = new JDBCConn();
}
return jdbcConn;
}
}
public class JDBCConnDemo{
public static void main(String args[]){
JDBCConn jdbcConn = this.initInstance();
this.getExecute(sql,"value");
}
}