JDBC
JDBC(Java DataBase Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口组成。JDBC提供了一种基准,据此可以构建更高级的工具和接口,使数据库开发人员能够编写数据库应用程序,同时,JDBC也是个商标名。
JAVA连接数据库:
步骤如下:
1.首先需要导入数据库的jar包。
2.通过反射加载驱动。
3.建立数据库连接。
4.利用java代码对数据库进行操作。(用java代码对数据库进行增删改查的操作)
下面是代码:
public class BaseDao {
private static Connection connection;
private static PreparedStatement ps;
private ResultSet rs;
public static Connection getConnection(){
try {
Class.forName(Contants.DRIVER);
connection = DriverManager.getConnection(Contants.URL, Contants.USERS, Contants.PWD);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("数据库连接成功");
return connection;
}
//增加
public static int insert(String sql,Object[] object){
int num = 0;
connection = getConnection();
try {
ps = connection.prepareStatement(sql);
if (object !=null&&object.length>0) {
for (int i = 0; i < object.length; i++) {
ps.setObject((i+1), object[i]);
}
}
num = ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return num;
}
//删除
public static int del(String sql,int num2){
int num = 0;
connection=getConnection();
try {
ps=connection.prepareStatement(sql);
ps.setInt(1, num2);
num = ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return num;
}
//修改
public int update(String sql,Object[] object){
int num = 0;
connection = getConnection();
try {
ps = connection.prepareStatement(sql);
if (object !=null&&object.length>0) {
for (int i = 0; i < object.length; i++) {
ps.setObject((i+1), object[i]);
}
}
num = ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return num;
}
//查询
public static ResultSet getSelect(String sql,Object[] objects){
try {
ps=getConnection().prepareStatement(sql);
if(objects!=null&& objects.length>0){
for (int i = 0; i < objects.length; i++) {
ps.setObject((i+1),objects[i]);
}
}
rs=ps.executeQuery();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rs;
}
}
//关闭
public static void close(Connection con,Statement st,ResultSet rs){
if (con!=null) {
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}if (st!=null) {
try {
st.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}if (rs!=null) {
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public interface DeptDao {
//增加
public int insert(Dept dept);
//删除
public int delete(int did);
//修改
public int update(Dept dept);
//查询全部
public List<Dept> selectDept();
}
public class Dept {
private int did;
private String dname;
private String dage;
private String desc;
public int getDid() {
return did;
}
public void setDid(int did) {
this.did = did;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public String getDage() {
return dage;
}
public void setDage(String dage) {
this.dage = dage;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public Dept(int did, String dname, String dage, String desc) {
this.did = did;
this.dname = dname;
this.dage = dage;
this.desc = desc;
}
public Dept() {
}
@Override
public String toString() {
return “Dept [did=” + did + “, dname=” + dname + “, dage=” + dage
+ “, desc=” + desc + “]”;
}
}
public class DeptImpl implements DeptDao{
BaseDao bd = new BaseDao();
@Override
public int insert(Dept dept) {
String sql = “insert into dept values(?,?,?,?)”;
Object[] object = {dept.getDid(),dept.getDname(),dept.getDage(),dept.getDesc()};
int num = bd.insert(sql, object);
return num;
}
@Override
public int delete(int id) {
String sql = “delete from dept where did=?”;
int num = bd.del(sql, id);
return num;
}
@Override
public int update(Dept dept) {
String sql = "update dept set dname=? where did=?";
Object[] object ={dept.getDname(),dept.getDid()};
int num = bd.update(sql, object);
return num;
}
@Override
public List<Dept> selectDept() {
String sql="select *from stu";
ResultSet rs=bd.getSelect(sql,null);
List<Dept> list=new ArrayList<Dept>();
try {
while(rs.next()){
Dept dept=new Dept(rs.getInt("sid"),rs.getString("sname"),rs.getInt("ssex"),rs.getInt("sage"),rs.getString("shobby"));
list.add(dept);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
}