jdbc连接数据库的7大步骤(以通过jdbc连接实现数据的插入、更新为例)

package cn.project_eg01;
import org.junit.Test;
import java.sql.DriverManager;
import java.sql.SQLException;


import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;


public class Connect01 {

private static String url = "jdbc:mysql://localhost:3306/myschool";
private static String user = "root";
private static String password = "214545";

@test
public static void testcreate01() {
Connection conn=null;
Statement stmt=null;
try {
// 1.注册驱动连接
Class.forName("com.mysql.jdbc.Driver");
// 2.建立连接
conn=(Connection) DriverManager.getConnection(url, user, password);
// 3.创建statement
stmt=(Statement) conn.createStatement();
// 4.准备sql语句
String sql = "INSERT INTO cat(id,name,health,love,strain) VALUES('002','小花',100,90,'雪纳瑞')";
// 5.执行sql语句
int count = stmt.executeUpdate(sql);
// 6.输出
System.out.println("输出了" + count + "行");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
throw new RuntimeException(e);
}finally{
// 7.关闭连接
if(stmt!=null)
try {
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new RuntimeException(e);
}
if(conn!=null)
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new RuntimeException(e);
}
}


}



// *********更新
public static void testupdate03() {
Connection conn=null;
Statement stmt=null;
// String setname="小白";
// int id=002;
try {
// 1.注册驱动连接
Class.forName("com.mysql.jdbc.Driver");
// 2.建立连接
conn=(Connection) DriverManager.getConnection(url, user, password);
// 3.创建statement
stmt=(Statement) conn.createStatement();
// 4.准备sql语句
// String sql = "UPDATE cat SET name='"+setname+"' WHERE id="+id+"";
String sql ="UPDATE cat SET name='小白' WHERE id=002";
// 5.执行sql语句
int count = stmt.executeUpdate(sql);
// 6.输出
System.out.println("输出了" + count + "行");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
throw new RuntimeException(e);
}finally{
// 7.关闭连接
if(stmt!=null)
try {
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new RuntimeException(e);
}
if(conn!=null)
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new RuntimeException(e);
} }
}

// ********主函数*****
public static void main(String[] args) {
// TODO Auto-generated method stub
//System.out.println("连接成功!");
testcreate01();
testinsert02();
testupdate03();
}


public static void testinsert02() {
Connection conn=null;
Statement stmt=null;
try {
// 1.注册驱动连接
Class.forName("com.mysql.jdbc.Driver");
// 2.建立连接
conn=(Connection) DriverManager.getConnection(url, user, password);
// 3.创建statement
stmt=(Statement) conn.createStatement();
// 4.准备sql语句
String sql = "INSERT INTO cat(id,name,health,love,strain) VALUES('003','小花',100,90,'雪纳瑞')";
// 5.执行sql语句
int count = stmt.executeUpdate(sql);
// 6.输出
System.out.println("输出了" + count + "行");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
throw new RuntimeException(e);
}finally{
// 7.关闭连接
if(stmt!=null)
try {
stmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new RuntimeException(e);
}
if(conn!=null)
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new RuntimeException(e);
}
}




}



}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值