JDBC练习
作者:互联网
package cn.itcast.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class jdbcDemo1 {
public static void main(String[] args) {
Statement stmt=null;
Connection conn=null;
try {
//注册驱动
Class.forName("com.mysql.jdbc.Driver");
//定义sql语句
String sql="insert into account values (null,'王五',3000)";
//连接数据库,获取Connection对象
conn = DriverManager.getConnection("jdbc:mysql:///db1","root","root");
//获取sql对象 Statement
stmt = conn.createStatement();
int count = stmt.executeUpdate(sql);
System.out.println(count);
if (count>0){
System.out.println("添加成功");
}else{
System.out.println("添加失败");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException 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();
}
}
}
}
}
标签:JDBC,练习,stmt,printStackTrace,sql,catch,null,conn 来源: https://www.cnblogs.com/Sym8023/p/15830295.html