简单入门JDBC
1.java与jdbc
jdbc出现之前,程序员对数据库进行处理时采用不同的方法,很大程度上浪费了开发成本。于是SUN公司为了简化、统一对数据库的操作,定义了一套Java操作数据库的规范,称为JDBC。
2.初步了解JDBC
重点来了,,,
package JDBC;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JdbcUtil {
private final static String UserUrl ="jdbc:mysql://localhost:3306/java80";
private final static String User="root";
private final static String UserPassword="root";
private final static String ClassName="com.mysql.jdbc.Driver";
static Connection con;
static Statement sta;
static ResultSet re;
static{
try {
Class.forName(ClassName);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static Connection Util() throws SQLException{
con = DriverManager.getConnection(UserUrl, User, UserPassword);
return con;
}
public static void Close() throws SQLException {
if(con!=null) {
con.close();
}
if(sta!=null) {
sta.close();
}
if(re!=null) {
re.close();
}
}
public static void main(String[] args) throws SQLException {
Connection con = JdbcUtil.Util();
Statement sta = con.createStatement();
ResultSet re = sta.executeQuery("select * from user");
while(re.next()) {
System.out.print(re.getInt("id")+" ");
System.out.print(re.getString("user")+" ");
System.out.print(re.getString("password")+"\n");
}
JdbcUtil.Close();
}
}
- 简单的程序已经送上,先别往下看,先自己理解理解,说不定下面的废话就不用看了哪!
3.进一步了解JDBC
JDBC常用接口如下,下面几个类的使用是学习JDBC的重中之中。
- 1.Class.forName(“com.mysql.jdbc.Driver”);
1)在上述程序中可以看到这端代码,这句代码是对程序的加载,在编程中要连接数据库,必须先装载特定厂商的数据库驱动程序,对于不同的数据库,拥有不同的Driver方式,由数据库厂商提供,我们直接应用就对了。()
- 2.Connection接口
con = DriverManager.getConnection(UserUrl, User, UserPassword);
1)上面部分程序进行了简单的处理,返回值应该是Connection,但为了满足全局使用的范畴,在开头部分已经提前声明;
2)其中UserUrl, User, UserPassword,这三个域主要是负责连接数据库的重要部分,对于UserUrl传递的是是与数据库连接的桥梁,而User,UserPassword是打开连接之门的钥匙
- 3.Statement接口
1)其实Statement这个接口很容易理解,通过Connection接口创建好了桥梁之后需要传递两者之间的数据。Statement就起到这样的作用,用来传递两者间的数据。
2)对于Statement接口现在已经很少用了,为了防止sql注入问题通常采用preparedStatement接口(这里不多说了,需要用到的话,自己去学习一下吧)
- 4.ResultSet接口
1)ResultSet接口通常用来处理sql语句,并将获得的数据进行传递出去,相当于把Statement上的货物进行装卸;
JDBC的增删改查
注意需要用到上面的JdbcUtil.Util()
package dao;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import JDBC.JdbcUtil;
import JDBC.User;
public class Dao {
private static Connection con;
private static Statement sta;
private static ResultSet re;
static {
try {
con =JdbcUtil.Util();
sta = con.createStatement();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//增加数据
public static int Increase() throws Exception {
int re =
sta.executeUpdate("insert into user(id,user,password) value(6,'ddd','111')");
System.out.println(re);
return re;
}
//删除数据
public static void Drop(int n) throws Exception {
int re = sta.executeUpdate("delete from user where id = " + n);
if(re==1) {
System.out.println("删除成功!");
}else {
System.out.println("删除失败!");
}
}
//修改数据
public static void Update() throws SQLException {
int re = sta.executeUpdate("Update user set id=8 where id=5");
if(re==1) {
System.out.println("数据修改成功!");
}else {
System.out.println("数据修改失败!");
}
}
//查找数据
public static void Query() throws SQLException {
ResultSet re = sta.executeQuery("select * from user");
while(re.next()) {
System.out.print(re.getInt("id")+" ");
System.out.print(re.getString("user")+" ");
System.out.print(re.getString("password")+"\n");
}
}
public static void main(String[] args) throws Exception {
Dao a = new Dao();
a.Query();
JdbcUtil.Close();
}
}
- 增删改查基本上可以满足很多程序的要求了,需要其他需求的话,就要继续学啦,加油💪!
mysql入门可参考简单了解Mysql