摘要:
数据库可以帮助我们对数据进行持久化存储,所以我们需要学会如何去使用数据库,使用JAVA对数据库进行增删改查操作。
测试数据库:
person:
数据库操作代码:
package com.sql;
import java.sql.*;
public class DataBase {
Connection conn = null;
Statement st = null;
public void Connect() throws
SQLException, java.lang.ClassNotFoundException {
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test";
String user = "root";
String password = "123456";
conn = DriverManager.getConnection(url, user, password);
System.out.println("链接数据库成功!");
} catch (Exception e) {
System.out.println("链接数据库失败!");
System.out.println(e.getMessage());
}
}
public void addOnePerson() {
try {
st = conn.createStatement();
st.executeUpdate("insert into person values (5,'xiaoming','girl')");
System.out.println(" addOnePerson()添加信息成功!");
} catch (SQLException e) {
System.out.println("添加信息失败!");
System.out.println(e.getMessage());
}
}
public void queryPerson() {
try {
st = conn.createStatement();
String StrSQL = "select * from person";
ResultSet rs = st.executeQuery(StrSQL);
System.out.println("person表的数据如下:");
System.out.println("----------------!");
System.out.println("--编号" + " --姓名" + "--性别");
System.out.println("-------------------");
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
String sex = rs.getString("sex");
System.out.println("--" + id + " --" + name + "--" + sex);
}
} catch (SQLException e) {
System.out.println("加载失败!");
System.out.println(e.getMessage());
}
}
public void delOnePerson() {
try {
st = conn.createStatement();
String StrSQL = "delete from person where id = 5";
Boolean flag = st.execute(StrSQL);
System.out.println("删除成功!");
} catch (SQLException e) {
System.out.println("加载失败!");
System.out.println(e.getMessage());
}
}
public void addPerson(int id, String name, String sex) {
try {
String sql = "insert into person(id,name,sex) values (?,? ,?)";
PreparedStatement pst = conn.prepareStatement(sql);
pst.setInt(1, id);
pst.setString(2, name);
pst.setString(3, sex);
pst.executeUpdate();
System.out.println("addPerson()添加信息成功!");
} catch (SQLException e) {
System.out.println("添加信息失败!");
System.out.println(e.getMessage());
}
}
public void delPerson(int id) {
try {
st = conn.createStatement();
String StrSQL = "delete from person where id =" + id;
Boolean flag = st.execute(StrSQL);
System.out.println("删除" + id + "一行成功!");
} catch (SQLException e) {
System.out.println("加载失败!");
System.out.println(e.getMessage());
}
}
public void close_connection() {
try {
st.close();
conn.close();
System.out.println("数据库关闭成功!");
} catch (Exception e) {
System.out.println("数据库失败!");
System.out.println(e.getMessage());
}
}
}
主程序测试:
package com.sql;
import java.sql.SQLException;
public class MainMySQL {
/**
* @param args
*/
public static void main(String[] args)throws SQLException,java.lang.ClassNotFoundException{
System.out.println("使用MySQL实例");
DataBase db=new DataBase();
db.Connect();
db.addOnePerson();
db.addPerson(1,"Alan","boy");
db.queryPerson();
db.delOnePerson();
db.delPerson(1);
db.close_connection();
}
}
数据库操作是一个非常复杂的过程,其中牵扯许多数学知识如笛卡尔积等,不光光是简单的增删改查,可以关注后续可能会更新数据库各种优化操作,减少时间复杂度实验。