jdbc基本操作

package cn.itcast.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
* @author newcityman
* @date 2019/8/12 - 23:40
*/
public class JDBCDemo02 {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db1", "root", "123");
String sql ="insert into account values(null,'newcityboy','1500.00')";
stmt = conn.createStatement();
int i = stmt.executeUpdate(sql);
if(i>0){
System.out.println("添加成功");
}else {
System.out.println("添加失败");
}
}catch (Exception e){
e.printStackTrace();
}finally{
if(stmt!=null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

}


}
}
posted on 2019-08-12 23:55  小猪_佩奇  阅读(159)  评论(0编辑  收藏  举报