标签:
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
标签:
原文地址:http://www.cnblogs.com/lzhp/p/5551704.html