码迷,mamicode.com
首页 > 数据库 > 详细

JDBC

时间:2016-06-02 07:22:11      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:

1.Connection

DriverManager.getConnection(prop.getProperty("url"),
prop.getProperty("username"), prop.getProperty("password"))

 

2.PreStatement

    con = JdbcUtils.getConnection();
            String sql = "insert into stu value(?,?,?,?)";
            pstmt = con.prepareStatement(sql);
            pstmt.setString(1, student.getNumber());
            pstmt.setString(2, student.getName());
            pstmt.setInt(3, student.getAge());
            pstmt.setString(4, student.getGender());
            pstmt.executeUpdate();

3.JDBCUtils

private static final String dbconfig = "dbconfig.properties";
    private static Properties prop = new Properties();
    static {
        try {
            InputStream in = Thread.currentThread().getContextClassLoader()
                    .getResourceAsStream(dbconfig);
            prop.load(in);
            Class.forName(prop.getProperty("driverClassName"));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static Connection getConnection() {
        try {
            return DriverManager.getConnection(prop.getProperty("url"),
                    prop.getProperty("username"), prop.getProperty("password"));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

4.dbconfig.properties(如果按照上面的路径写的话 ,文件需要写在包的外面)

driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/stu?useUnicode=true&characterEncoding=UTF8
username=root
password=123456

 

JDBC

标签:

原文地址:http://www.cnblogs.com/lzhp/p/5551704.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!