在写代码之前我们首先导入架包,我们在src目录下新建一个名为lib得文件夹,将架包导入项目中。
新建一个properties文件:
里面写上关于数据库连接所需资料:
user=root//数据库得用户名
password=123456//连接数据库得密码
url=jdbc:mysql://localhost:3306/rb1801 //数据库获取路径
url书写格式是 JDBC连接方式://本机地址//数据库端口号//要连接得数据库得地址;
接下来我们开始写代码:
package com.cy
import java.i0.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SqlException;
import java.sql.Staatement;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import com.musql.jdbc.Driver;
//创建一个工具类实现连接数据库和实现数据库得增删改查;
public class JDBCUtils{
//1、获取数据库连接对象
public static Connection getConnection(){
Connection co=null;
try{
Driver driver=new Driver();
Properties info new Properties();
InputStream in = JDBCUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");
info.load(in);
String url =info.getProperty("url");
co=driver.connect(url,info);
System.out.println(co);
}catch(Exception e){
e.printStackTrace();
}
return co;
}
//关闭数据库连接资源
public static void closeConnection(Connection conn,Ctatement sta,ResultSet rs){
if(rs!=null){
try{
rs.close;
}catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(sta != null){
try {
sta.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//插入、修改、删除数据;
//这里得sql语句是从主方法中传入得;
public static void update(String sql){
//调用前面写得方法连接数据库
Connection conn = getConnection();
//创建Statement对象;
Statement statement = null;
try {
statement = conn.createStatement();
//调用executeUpdate();执行数据库语句
statement.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
//关闭资源,因为没有使用ResultSet所以传入一个空得元素
closeConnection(conn, statement,null);
}
}
//查询某一个学生得数据
public static Student selectStuOne(String sql)
{
Connection conn = getConnection();
Statement statement = null;
ResultSet rs = null;
Student s = null;
try {
statement = conn.createStatement();
rs = statement.executeQuery(sql);
if(rs.next())
{
int id = rs.getInt("id");
String name = rs.getString("name");
int age = rs.getInt("age");
s = new Student(name, age, id);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
closeConnection(conn, statement,rs);
}
return s;
}
//查询所有数据
public static List<Student> selectAll(){
Connection conn = getConnection();
Statement statement = null;
ResultSet rs = null;
List<Student> stus = new ArrayList<Student>();
String sql = "select * from student";
try {
statement = conn.createStatement();
rs = statement.executeQuery(sql);
while(rs.next())
{
int id = rs.getInt("id");
String name = rs.getString("name");
int age = rs.getInt("age");
stus.add(new Student(name, age, id));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
closeConnection(conn, statement,rs);
}
return stus;
}
}
创建Student类;
package com.cy;
public class Student {
private String name;
private int age;
private int id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Student(String name, int age, int id) {
super();
this.name = name;
this.age = age;
this.id = id;
}
public Student() {
super();
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", id=" + id + "]";
}
}
创建测试类;
package com.cy;
import java.util.List;
public class Test {
public static void main(String[] args) {
//传入增加语句;
//String sql = "insert into student(name,age) values('李白',20)";
//String sql1 = "insert into student(name,age) values('阿珂',18)";
//传入删除语句
//String sql = "delete from student where id = 8";
//查询一个学生得数据
//String sql="select * from student where id = 4";
//传入修改语句
//String sql = "update student set name='荆轲' where id = 9";
//查询所有得数据
List<Student> stus = JDBCUtils.selectAll();
for(Student s:stus)
{
System.out.println(s);
}
}
}