您的位置:首页 > 数据库

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

2017-09-01 15:39 639 查看
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);
}
}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息