eclipse+jdbc+mysql+statement实现数据库的增删改

来源:互联网 发布:淘宝运营职位 编辑:程序博客网 时间:2024/05/29 14:25

一、数据库增加数据

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




public class testInsert {
public static void testInsert(){
//声明连接对象,语句对象,以便可以在finally中关闭
Connection conn=null;
Statement stmt=null;
try{
//加载驱动类,不同数据库软件驱动类不同
Class.forName("com.mysql.jdbc.Driver");
//使用DriverManager获得连接对象,其中url每个数据库不同
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/user?useUnicode=true&characterEncoding=utf8",
"root","1234");
//使用连接对象,获得语句对象Statement
stmt=conn.createStatement();
//使用语句对象的excuteUpdate方法,执行insert,update,delete
String sql="insert into user values('wangxh007','1','2','89990433','2015-01-07 07:18:59')";
stmt.executeUpdate(sql);
}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();
}
}
}
}

public static void main(String[] args){
testInsert();
}
}

二、数据库删除

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


public class testDelete {


public static void testDelete(){
//声明连接对象,语句对象,以便可以在finally中关闭
Connection conn=null;
Statement stmt=null;
try{
//加载驱动类,不同数据库软件驱动类不同
Class.forName("com.mysql.jdbc.Driver");
//使用DriverManager获得连接对象,其中url每个数据库不同
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/user?useUnicode=true&characterEncoding=utf8",
"root","1234");
//使用连接对象,获得语句对象Statement
stmt=conn.createStatement();
//使用语句对象的excuteUpdate方法,执行insert,update,delete
String sql="delete from user where username='wangxh007'";
stmt.executeUpdate(sql);
}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();
}
}
}
}

public static void main(String[] args){
testDelete();
}
}

三、数据库更改

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


public class testUpdate {


public static void testUpdate(){
//声明连接对象,语句对象,以便可以在finally中关闭
Connection conn=null;
Statement stmt=null;
try{
//加载驱动类,不同数据库软件驱动类不同
Class.forName("com.mysql.jdbc.Driver");
//使用DriverManager获得连接对象,其中url每个数据库不同
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/user?useUnicode=true&characterEncoding=utf8",
"root","1234");
//使用连接对象,获得语句对象Statement
stmt=conn.createStatement();
//使用语句对象的excuteUpdate方法,执行insert,update,delete
String sql="update user set status=1 where username='wangxh006';";
stmt.executeUpdate(sql);
}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();
}
}
}
}

public static void main(String[] args){
testUpdate();
}
}


心得:

1、需要在WEB-INF的lib目录下加载mysql-connector-java-5.1.7-bin.jar。

2、如果出现java-web应用没有run on application这个选项,可能是main方法出了问题。本次编程中将string[] args中的方括弧少打了,故出现问题。

3、代码思路为:连接对象,加载驱动,连接数据库,编写sql语句,执行sql语句,关闭对象。



阅读全文
0 0
原创粉丝点击