一。准备工作:
1.操作数据库的JDBC.jar包,下载地址:http://download.csdn.net/detail/qq_33750826/9671725
2.如果你的电脑为安装mysql请安装mysql,下载地址:http://download.csdn.net/detail/qq_33750826/9671736
安装方式:http://jingyan.baidu.com/article/f79b7cb35c0f439144023e38.html
下载地址附有安装失败的解决办法
3.下载与java配合使用的数据库,下载地址:http://download.csdn.net/detail/qq_33750826/9671745
记住上面mysql成功正确之后才可以使用sqlyog,否则创建连接一直会失败的
4.创建java Project将下载的jdbc包导入到项目中:点击项目->右键->Build Path->Configure Build Path->libraries->Add External JARs,找到下载的jdbc包路径导入,点击确定->Apply
5.安装sqlyog并能够连接上数据库之后首先在sqlyog数据库中创建一个数据库:
之后创建表:
这是我已经创建好的表
、
准备工作到上面就结束了。
如果上面都做成功了那么下面进入代码阶段:
二.代码部分
1.找到准备工作中创建的java Project项目,我先写好了一个辅助类:
package com.example.javajdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DbHelper {
/**
* 建立链接
* */
public static Connection getconnection() {
try{
//获得操作管理者的驱动程序,也就是DriverManager
Class.forName("com.mysql.jdbc.Driver");
//通过jdbc连接mysql,获得连接对象
return DriverManager.getConnection("jdbc:mysql://localhost:3306/wttest", "root", "123456");
}catch (Exception e) {
e.printStackTrace();
return null;
}
}
//关闭连接
public static void closeConnectionAndStatement(Connection con,Statement st,ResultSet oSet){
try {
if(st!=null)st.close();
if(con!=null)con.close();
if (oSet!=null) {
oSet.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
2.插入:
package com.example.javajdbc;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class InsertJDBC {
public static void main(String[] args) {
DbHelper helper=new DbHelper();
Connection connection=helper.getconnection();
insertTwo(connection);
}
public static void insertOne(Connection connection){
String sql="insert into people(name,age,sex) values('jhl','100','male')";
PreparedStatement ps = null;
try {
//将sql语句提交到数据库进行预编译
ps=connection.prepareStatement(sql);
int i=ps.executeUpdate();
if (i>0) {
System.out.println("添加成功");
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
if (connection!=null) {
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (ps!=null) {
try {
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public static void insertTwo(Connection connection){
String sql="insert into people(name,age,sex) values(?,?,?)";
PreparedStatement ps = null;
try {
//将sql语句提交到数据库进行预编译
ps=connection.prepareStatement(sql);
ps.setString(1, "jhl2");
ps.setString(2, "102");
ps.setString(3, "female");
int i=ps.executeUpdate();
if (i>0) {
System.out.println("添加成功");
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
if (connection!=null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (ps!=null) {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
3.更新
package com.example.javajdbc;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Savepoint;
import javax.naming.ConfigurationException;
public class UpdateJDBC {
public static void main(String[] args) throws SQLException {
DbHelper helper = new DbHelper();
Connection connection = helper.getconnection();
//updateOne(connection);
updateTwo(connection);
}
public static void updateOne(Connection connection) throws SQLException {
String sql = "update people set age='200' where name='jhl2'";
/**
* 自动提交为false表示如果没有执行commit()方法则不会提交更新 和没有执行rollback()取消savepoint的更新
* 如果连接处于自动提交模式下,则它的所有 SQL 语句将被执行并作为单个事务提交。 否则,它的 SQL 语句将聚集到事务中,直到调用
* commit 方法或 rollback 方法为止。 默认为true,自动提交更新
*/
connection.setAutoCommit(false);
PreparedStatement ps = connection.prepareStatement(sql);
int i = ps.executeUpdate();//对于这个返回值表示你提供的sql语句要更新了多少行,如果没有更新则返回0,否则返回更新的数量
System.out.println(i);
if (i >= 0) {
connection.commit();
connection.close();
ps.close();
System.out.println("更新成功");
} else {
connection.close();
ps.close();
System.out.println("更新失败");
}
}
public static void updateTwo(Connection connection) throws SQLException {
String sql = "update people set age='400' where age='4100'";
connection.setAutoCommit(false);
/**
* 设置一个保存点,方便取消某些更新
*/
Savepoint savepoint = connection.setSavepoint();
PreparedStatement ps = connection.prepareStatement(sql);
//对于这个返回值表示你提供的sql语句要更新了多少行,如果没有更新则返回0,否则返回更新的数量
int i=ps.executeUpdate();
System.out.println(i);
if (i >=0 ) {
//如果添加了此行就算你commit了还是不会更新数据,因为你在commit之前已经roolback回滚了,就是取消更新了
//connection.rollback(savepoint);
connection.commit();
connection.close();
ps.close();
System.out.println("更新成功");
} else {
connection.rollback(savepoint);
connection.close();
ps.close();
System.out.println("更新失败");
}
}
}
4.查询:
将查询对象放在people类中
package com.example.javajdbc;
public class People {
String name;
String age;
String sex;
public People(String name, String age, String sex) {
super();
this.name = name;
this.age = age;
this.sex = sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
4.1
package com.example.javajdbc;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
public class SelectJDBC {
public static void main(String[] args) throws SQLException {
DbHelper helper = new DbHelper();
Connection connection = helper.getconnection();
// selectOne(connection);
selectTwo(connection);
}
public static void selectOne(Connection connection) throws SQLException {
String sql = "select * from people";
PreparedStatement ps = connection.prepareStatement(sql);
ResultSet rSet = ps.executeQuery();
while (rSet.next()) {
System.out.println(rSet.getString("age"));
}
connection.close();
ps.close();
}
public static void selectTwo(Connection connection) throws SQLException {
String sql = "select * from people";
PreparedStatement ps = connection.prepareStatement(sql);
ResultSet rSet = ps.executeQuery();
People people;
ArrayList list = new ArrayList();
while (rSet.next()) {
String name = rSet.getString("name");
String age = rSet.getString("age");
String sex = rSet.getString("sex");
people = new People(name, age, sex);
list.add(people);
}
Iterator iterator = list.iterator();
while (iterator.hasNext()) {
People people2 = iterator.next();
System.out.println(people2.getName() + "," + people2.getAge() + "," + people2.getSex());
}
}
}
5.删除:
package com.example.javajdbc;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class DeleteJDBC {
public static void main(String[] args) throws SQLException {
DbHelper helper=new DbHelper();
Connection connection=helper.getconnection();
deleteOne(connection);
}
public static void deleteOne(Connection connection) throws SQLException{
String sql="delete from people where age=400";
PreparedStatement ps=connection.prepareStatement(sql);
//对于这个返回值表示你提供的sql语句要delete了多少行,如果没有delete则返回0,否则返回delete的数量
int i=ps.executeUpdate();
if (i>=0) {
ps.close();
connection.close();
System.out.println("删除成功");
}else {
ps.close();
connection.close();
System.out.println("删除失败");
}
}
}