首页 > 数据库技术 > 详细

JDBC

时间:2016-06-02 07:21:47      阅读:274      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!