JDBC
Posted haiziguo
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JDBC相关的知识,希望对你有一定的参考价值。
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的主要内容,如果未能解决你的问题,请参考以下文章