JDBC使用步骤
//1.导入驱动程序
//2.加载数据库的驱动
Class.forName("com.mysql.jdbc.Driver");
//3.获取数据库联接(数据库地址,数据库用户名,数据库密码)
//MySQL数据库的URL→jdbc:mysql://localhost:3306/mydb
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/stu", "root", "root");
//4.获得Statement对象:操作数据库(insert update delete select)
Statement state= conn.createStatement();
//5.执行SQL语句
//String sql = "INSERT INTO t_user(username,`password`) VALUES('mysql','000000')";
//String sql = "update t_user set username = 'Apple' where id = 10";
String sql = "delete from t_user where id = 6";
int rows = state.executeUpdate(sql);//executeUpdate()方法用于执行insert,update,delete语句不能用于查询
ResultSet set=state.executeQuery(sql); //查询
while(set.next()){
int id=set.getInt("id");
String name=set.getString("name");
System.out.println(id+"\t"+name);
}
//6.释放联接
state.close();
conn.close();
例子一:数据库的增删改查
public class Test1 {
public static void main(String[] args) {
Connection con=null;
Statement state=null;
int result=0;
try {
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/stu","root","root");
state=con.createStatement();
//增
//String sql="insert into stu(`name`,age,address) values('rose',50,'UK')";
//删
String sql="delete from stu where id=1";
//改
//String sql="update stu set `name`='lucy' where `name`='rose'";
result=state.executeUpdate(sql);
System.out.println(result);
//查
String sqlString="select id,`name`,age,address from stu";
ResultSet set=state.executeQuery(sqlString);
while (set.next()) {
int id=set.getInt("id");
String name=set.getString("name");
int age=set.getInt("age");
String address=set.getString("address");
System.out.println(id+"\t"+name+"\t"+age+"\t"+address);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if (state!=null) {
try {
state.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if (con!=null) {
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
}
}
例子二:这种方法是建立一个DBHelper类,能实现增删改的功能,不能实现查,只需在其他类里调用即可。public class DBHelper {
private final String DRIVER="com.mysql.jdbc.Driver";
private final String URL="jdbc:mysql://localhost:3306/stu";
private Connection getConnection() throws Exception{
Class.forName(DRIVER);
return DriverManager.getConnection(URL,"root","root");
}
public int execute(String sql){
Connection connection=null;
Statement state=null;
int result=0;
try {
connection=getConnection();
state=connection.createStatement();
result=state.executeUpdate(sql);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
close(connection,state);
}
return result;
}
private void close(Connection connection, Statement state) {
// TODO Auto-generated method stub
if (state!=null) {
try {
state.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if (connection!=null) {
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
}
当要使用时,在测试类QueryTest里测试即可。public class QueryTest {
public static void main(String[] args) {
String sqlString="insert into stu(`name`,age,address) values('DMC',23,'CA')";
DBHelper helper=new DBHelper();
int i= helper.execute(sqlString);
System.out.println(i);
}
}
最后上知识点图