JDBC相关

//原生jdbc操作案例

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Scanner;

/*
 *  Java实现一个登录功能
 *    可以在控制台输入用户名和密码
 *    查询数据表,查询到数据,登录成功,否则登录失败
 */
public class JDBCDemo {
    public static void main(String[] args) throws Exception{
        Scanner sc = new Scanner(System.in);
        //注册驱动
        Class.forName("com.mysql.jdbc.Driver");
        //获取数据库连接 
        Connection con = 
                DriverManager.getConnection("jdbc:mysql://localhost:3306/***", "***", "***");
        //获取SQL语句执行对象
        Statement stat = con.createStatement();
        
        //键盘输入
        String user = sc.nextLine();
        String pass = sc.nextLine();
        
        //登录查询的SQL
        String sql = "SELECT * FROM users WHERE username='"+user+"' AND PASSWORD='"+pass+"'";
        
        System.out.println(sql);
        
        //执行查询语句,返回结果集对象
        ResultSet rs =  stat.executeQuery(sql);
        //判断是否有结果集
        while(rs.next()){
            System.out.println(rs.getString("username")+"   "+rs.getString("password"));
        }
        rs.close();
        stat.close();
        con.close();
    }
}

//封装后的DBUtil工具

 1 /*
 2  *  Apache提供的数据库连接池 dbcp
 3  *  javax.sql.DataSource接口,是所有连接池的标准规范
 4  *  接口中的方法  Connection getConnection()
 5  *  
 6  *  org.apache.connons.dbcp.BasicDataSource类,实现接口DataSource
 7  *  设置数据库连接的四大信息
 8  *    驱动类,url,username,password
 9  *  BasicDataSource类方法 setXXX设置
10  */
11 public class DBCPUtils {
12     //成员位置,创建接口实现类对象
13     private static BasicDataSource dataSource = new BasicDataSource();
14     static{
15         //设置数据库连接四大信息    
16         dataSource.setDriverClassName("com.mysql.jdbc.Driver");
17         dataSource.setUrl("jdbc:mysql://localhost:3306/***");
18         dataSource.setUsername("****");
19         dataSource.setPassword("****");
20     }
21     
22     public static Connection getConnection () throws SQLException{
23         
24         //调用接口实现类对象BasicDataSource方法,返回连接
25          return  dataSource.getConnection();
26     }
27 }

//C3p0工具

 1 /*
 2  *  连接池工具类 c3p0
 3  *  找到接口DataSource实现类对象
 4  *  com.mchange.v2.c3p0.ComboPooledDataSource类实现接口
 5  */
 6 public class C3P0Utils {
 7     //成员位置,创建DataSource接口实现类对象--读取配置文件
 8     private static ComboPooledDataSource dataSource = new ComboPooledDataSource();
 9     /*static{
10         try{
11             //设置数据库连接四大信息
12             dataSource.setDriverClass("com.mysql.jdbc.Driver");
13             dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/****");
14             dataSource.setUser("****");
15             dataSource.setPassword("****");
16         }catch(Exception ex){}
17     }*/
18     
19     public static DataSource getDataSource(){
20         return dataSource;
21     }
22     
23     /*
24      * 提供方法,返回数据库连接对象
25      */
26     public static Connection getConnection() throws SQLException{
27         return dataSource.getConnection();
28     }
29     
30     
31     /*
32      * 定义静态方式,实现释放资源
33      */
34     public static void close(ResultSet rs,Statement stat,Connection con){
35         if(rs!=null)
36             try {
37                 rs.close();
38             } catch (SQLException e) {
39                 e.printStackTrace();
40             }
41         
42         if(stat!=null)
43              try{
44                  stat.close();
45              }catch(Exception ex){}
46             
47             if(con!=null)
48                 try{
49                      con.close();
50                  }catch(Exception ex){}
51     }
52 }

//C3p0-config.xml内容

1 <?xml version="1.0" encoding="UTF-8"?>
2 <c3p0-config>
3     <default-config>
4         <property name="user">root</property>
5         <property name="password">root</property>
6         <property name="driverClass">com.mysql.jdbc.Driver</property>
7         <property name="jdbcUrl">jdbc:mysql:///***</property>
8     </default-config> 
9 </c3p0-config> 

 

posted @ 2018-08-11 11:15  it~沃克尔  阅读(158)  评论(0编辑  收藏  举报