mysql数据库连接小笔记----第一个



Connection conn = null;
Statement stmt = null;

try {

//加载驱动类
Class.forName("com.mysql.jdbc.Driver");

//建立连接,root为数据库的用户名,password为密码
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/myjdbc","root","password");
stmt = conn.createStatement();

//字符串要用单引号括起来
String sql = "delete from myfirsttb where name='yuderong'";
stmt.execute(sql);

} catch (Exception e) {
e.printStackTrace();
}finally{//关闭连接部分
try {
if (stmt != null) {
stmt.close();

} catch (Exception e2) {
}

try {
if (conn != null) {
conn.close();

} catch (Exception e2) {
}

}

你可能感兴趣的:(Java数据库连接)