java的一些基础方法(数据库连接)

java的一些基础方法(数据库连接)

 1  package  com.duduli.li.db;
 2 
 3  import  java.sql.Connection;
 4  import  java.sql.DriverManager;
 5  import  java.sql.ResultSet;
 6  import  java.sql.SQLException;
 7  import  java.sql.Statement;
 8 
 9  public   class  Conn {
10       static  Connection conn  =   null ;
11       static  Statement stat  =   null ;
12      ResultSet rs  =   null ;
13      
14      
15       public   static  Connection connect()  throws  InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException{
16          String username  =   " root " ;
17          String password  =   " adminadmin " ;
18          String url  =   " jdbc:mysql://localhost:3306/test " ;
19          String jdbcDriver  =   " com.mysql.jdbc.Driver " ;
20          Class.forName(jdbcDriver).newInstance();
21          System.out.println( " connect successful " );
22          conn  =  DriverManager.getConnection(url, username, password);
23           return  conn;
24      }
25      
26       public   static   void  colsed()  throws  SQLException{
27          stat.close();
28          conn.close();
29      }
30      
31       public  ResultSet query(String sql)  throws  SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException{
32          stat  =  connect().createStatement();
33          rs  =  stat.executeQuery(sql);
34           return  rs;
35      }
36      
37       public   boolean  execute(String sql)  throws  SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException{
38          stat  =  connect().createStatement();
39           return  stat.execute(sql);
40      }
41  }
42 

你可能感兴趣的:(java的一些基础方法(数据库连接))