JDBC的准备工作
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
* 测试跟数据库连接
* @author Administrator
*
*/
public class Demo01 {
public static void main(String[] args) {
Connection con = null;
try {
//加载驱动类
Class.forName("com.mysql.jdbc.Driver");
long start = System.currentTimeMillis();
//建立连接(连接内部对象其实包括了Socket对象,是一个远程的连接。比较耗时,这是Connection对象管理的一个要点)
//在开发中,为了提高效率,都会使用连接池来管理连接对象
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/testjdbc","root","529457099");
long end = System.currentTimeMillis();
System.out.println(con);
System.out.println((end-start));
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
if(con!=null){
con.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
对数据库的操作
对于数据库的操作有几个需要了解的,也就是传统的增、删、查、改:
增加一条记录:insert into 数据库名 (?,?,?…..列数参数名) values (?,?,?……对前面括号内的赋值)
查询一条记录:select * from 数据库名 where 列对应名 处理范围
- 删除一条记录:delete * from 数据库名 where 列对应名 处理范围
一、JDBC常用接口Statement
/**
* 使用Statement测试执行SQL语句,以及SQL注入问题(使用PreparedStatement就不会)
* @author Administrator
*
*/
public class Demo02 {
public static void main(String[] args) {
Connection con = null;
Statement stmt = null;
try {
//加载驱动类
Class.forName("com.mysql.jdbc.Driver");
//建立连接
con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/testjdbc","root","529457099");
stmt = con.createStatement();
// String name = "nime"; //为了拼装语句
// String sql = "insert into t_user (username,pwd,regTime) values ('"+name+"',4213,now())";
// stmt.execute(sql);
//测试SQL注入,有漏洞的话会被别人侵入你的数据库
//所谓SQL注入,就是通过把SQL命令插入到Web表单递交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令
String ID = "5 or 1=1"; //1=1是肯定成立的,一运行,就会把所有的数据全删除了
String sql = "delete from t_user where ID = 5"+ID;
stmt.execute(sql);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
if(con!=null){
stmt.close(); //后开的先关
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(con!=null){
con.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
二、JDBC常用接口PreparedStatement
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
/**
* 测试PreparedStatement的基本用法
* @author Administrator
*
*/
public class Demo03 {
public static void main(String[] args) {
Connection con = null;
PreparedStatement ps = null;
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/testjdbc","root","529457099");
String sql = "insert into t_user(username,pwd) values (?,?)"; //问号叫做占位符,这样可以避免SQL注入
ps = con.prepareStatement(sql);
// ps.setString(1, "nimade"); //前面是索引,也就是第几列,后面是值,如果没有数据,索引是从1开始而不是0
// ps.setString(2, "12346");
ps.setObject(1, "ninainaide");
ps.setObject(2, "4632");
ps.execute();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
if(con!=null){
ps.close(); //后开的先关
}
} catch (SQLException e) {
e.printStackTrace();
}try {
if(con!=null){
con.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
三、下面就是测试常用的Statement方法
- executeQuery():一般用来操作select语句,染回ResultSet结果集
- executeUpdate():运行insert/update/delete操作,返回更新的行数
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* 测试ResultSet结果集的具体用法
* @author Administrator
*
*/
public class Demo04 {
public static void main(String[] args) {
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/testjdbc","root","529457099");
String sql = "select * from t_user where id>?"; //问号叫做占位符,这样可以避免SQL注入,*表示所有的列数
ps = con.prepareStatement(sql);
ps.setObject(1, 3);
rs = ps.executeQuery();
while(rs.next()){
System.out.println(rs.getInt(1)+"\t"+rs.getString(2));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
if(con!=null){
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(con!=null){
ps.close(); //后开的先关
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if(con!=null){
con.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}