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);
}
}
}
}
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);
}
}
}
}
相关文章推荐
- WPF连接SQL数据库,将TextBox中填写的数据插入数据库中,并更新DataGrid表
- JDBC 数据库连接 创建表格、插入、查询、删除、修改数据 基本操作
- 通过jdbc向数据库中插入数据,并对密码进行加密
- Java:jdbc连接数据库插入中文数据乱码问题
- 连接数据库的工具类,实现数据的插入
- 使用JDBC连接MySQL数据库--典型案例分析(四)----更新和插入员工数据
- JDBC远程从一个MySql数据库中的一张表里面读出数据(这个数据库需要用SSH隧道连接,大约8W条数据),然后分别插入到另一个数据库中的两张表里
- jdbc连接数据库 实现步骤
- JDBC连接MySql数据库步骤 以及查询、插入、删除、更新等
- java通过jdbc连接数据库并在前端实现增删查改
- Java:jdbc连接数据库插入中文数据乱码问题
- 使用navicat8通过数据转移实现更新 和 数据库保存实现备份[图文]
- JDBC 连接数据库 具体步骤与实现
- JDBC应用程序连接数据库--插入数据并且获得相应自增长值的列
- 利用JDBC连接实现跨服务器跨数据库跨表数据传输
- java利用jdbc连接数据库之插入数据
- 通过代理类实现java连接数据库(使用dao层操作数据)实例分享
- 通过jdbc实现对数据库中数据的增删查改操作
- JDBC连接Mysql实现数据的插入、更改操作
- 用JDBC连接Vertica数据库后插入一行数据的例子源代码(接上篇)