1.在mysql数据库中新建一张表tbl_user
</pre><pre name="code" class="sql">create table tbl_user(
id int(11) unsigned not null auto_increment ,
name varchar(50) not null default ”,
password varchar(50)not null default ”,
email varchar(50) not null default ”,
primary key(id))
engine=InnoDB
default charset=utf8;
2.在表中添加数据
insert into tbl_user(id,name,password,email)
values
(1,’xiaohuang’,’xiaohuang’,’1023123@qq.com’),
(2,’xiaoming’,’xiaoming’,’12314@qq.com’);
3.在JDBCtest.java中对数据库进行增删查改的操作
package com.jiebaba.jdbc;
import java.sql.DriverManager;
import java.sql.ResultSet;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
public class JDBCtest {
public static Connection getConnection(){
Connection connection=null;
try {
Class.forName(“com.mysql.jdbc.Driver”);
connection=(Connection) DriverManager.getConnection
(“jdbc:mysql://localhost:3306/jsp_db”,”root”,”root”);
} catch (Exception e) {
e.printStackTrace();
}
return connection;
}
public static void insert(){
Connection connection=getConnection();
try {
String sql=”INSERT INTO tbl_user(name,password,email)”
+”VALUES(‘maoer’,’maoer’,’maoer@qq.com’)”;//插入记录
Statement statement=(Statement) connection.createStatement();
int count=statement.executeUpdate(sql);
System.out.println(“向数据表中插入了”+count+”条数据”);
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void update(){
Connection connection=getConnection();
try {
String sql=”UPDATE tbl_user SET email=’maoer@gmail.com’WHERE name=’maoer'”;
Statement statement=(Statement) connection.createStatement();
int count=statement.executeUpdate(sql);
System.out.println(“向数据表中更新了”+count+”条数据”);
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void delete(){
Connection connection=getConnection();
try {
String sql=”DELETE FROM tbl_user WHERE name=’maoer'”;
Statement statement=(Statement) connection.createStatement();
int count=statement.executeUpdate(sql);
System.out.println(“向数据表中删除了”+count+”条数据”);
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
insert();
update();
delete();
}
}