public static void main(String[] args) {
Class.forName("com.mysql.jdbk.Driver"); //加载驱动
DriverManager.reisterDriver(new Driver());// 注册驱动(可以省略)
Connection conn = DriverManager.getConnection(String url, String userName, String password);
//jdbc:mysql://localhost:3306/myDataBaseName
Statement stmt = conn.createStatement();//获取数据库操作对象一
PrepareStatement pstmt = conn.prepareStatement(String sql);//获取数据库操作对象二
}
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try{
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql:///myDataBaseName", "root", "root");
stmt = conn.createStatement();
stmt.executeUpdate("update table_name set xxx = yyy where zzz");
rs = stmt.executeQuery("select * from table");
while(rs.next()) {
//.....
}
}catch(Exception e) {}finally {
if(stmt != null) stmt.close();
if(conn != null) conn.close();
}
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |