一、SQL与JDBC
1,SQL驱动
步骤: (1)设置驱动包调用路径driver
(2)设置调用的URL
(3)设置用户名username
(4)设置用户密码userpass
(5)调用驱动包Class.forName(driver)
(6)创建获取连接的方法getConnection()
(7)配置连接:
conn = DriverManager.getConnection(url, username, userpass);
(8)创建关闭连接的方法:
CloseAll(Connection conn,Statement sta,ResultSet rs)
public class DBConnection {
//驱动
private static String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
private static String url = "jdbc:sqlserver://192.168.2.182:1433;databaseName=erp";
private static String username = "sa123456";
private static String userpass = "123456";
private static Connection conn = null;
//private static ResultSet rs = null;
static{
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//获取连接的方法
public static Connection getConnection(){
//判断指定的资源是否为空
if(conn==null){
try {
conn = DriverManager.getConnection(url, username, userpass);
return conn;
} catch (SQLException e) {
e.printStackTrace();
}
}
return conn;
}
//通用的关闭资源的方法
public static void CloseAll(Connection conn,Statement sta,ResultSet rs){
if(conn !=null)
try {
conn.close();
DBConnection.conn = null;
} catch (SQLException e) {
e.printStackTrace();
}
if(sta !=null)
try {
sta.close();
} catch (SQLException e) {
e.printStackTrace();
}
if(rs !=null)
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
2,运用JDBC设置数据库
步骤: (1)加载驱动:Class.forName(driver)
(2)连接数据库:
DriverManager.getConnection(url, username, userpass);
(3)创建SQL语言:sql = "select * from emp"
(4)创建执行SQL的载体:Statement sta = conn.createStatement();
(5)发送SQL语言到数据库:ResultSet rs = sta.executeQuery(sql);
(6)取出返回结果:
(7)关闭资源
注意:* 1,在数据库中是什么样的类型字段取出时就要以什么样的字段接收
* 2,在取出时传递的参数可以是下标号也可以是列名
* 3,在数据库中下标从1开始计算
public class JdbcDemo {
public static void main(String[] args) {
//1,加载驱动
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
//2,连接数据库
Connection conn = DriverManager.getConnection("jdbc:sqlserver://192.168.2.182:1433;databaseName=erp", "sa123456", "123456");
//3,清单(创建查询语句)
String sql = "select * from emp";
//4,创建执行SQL语句的载体
Statement sta = conn.createStatement();
//5,发送SQL语句到数据库中执行
ResultSet rs = sta.executeQuery(sql);
//循环的遍历rs指向的结果集
/*while(rs.next()){
int empno = rs.getInt(1);
//int empno = rs.getInt("empno");
String ename = rs.getString(2);
String job = rs.getString(3);
System.out.println(empno+" "+ename+" "+job);
}
*/
//把数据存入集合中
List<Emp> list = new ArrayList<Emp>();
Emp emp = null;
while(rs.next()){
emp = new Emp();
emp.setEmpno(rs.getInt("empno"));
emp.setEname(rs.getString("ename"));
emp.setJob(rs.getString("job"));
emp.setMgr(rs.getString("mgr"));
emp.setHiredate(rs.getString("Hiredate"));
emp.setSal(rs.getDouble("sal"));
emp.setComm(rs.getDouble("comm"));
emp.setDeptno(rs.getInt("deptno"));
//添加进集合
list.add(emp);
}
//遍历集合
Iterator<Emp>ite = list.iterator();
System.out.println("empno ename job mgr hiredate sal comm deptno");
while(ite.hasNext()){
Emp emp1 = ite.next();
System.out.println(emp1.toString());
}
//6.关闭开启的资源
conn.close();
sta.close();
rs.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
二、配置properties
1,配置properties类来设置驱动
优点:方便以后更改驱动信息,而不用去修改程序的内容
//创建properties类的对象(持久的属性集)
Properties p = new Properties();
//创建一个输入流指向要读取的文件
is = new FileInputStream("db.properties");
//加载文件流
p.load(is);
//获取指定的key所对应的value值
driver = p.getProperty("driver");
url= p.getProperty("url");
user = p.getProperty("user");
pass = p.getProperty("pass");
//加载驱动
Class.forName(driver);
2,获取连接
* DriverManager管理一组 JDBC 驱动程序的基本服务。
* getConnection()试图建立到给定数据库 URL 的连接,返回一个Connection类型变量
* Connection类的方法:
* prepareStatement()返回一个PreparedStatement对象----操作SQL语句
* prepareCall()返回一个CallableStatement对象-----操作存储过程语句 String sql = "{call 存储过程名(传递参数)}";
Connection conn = DriverManager.getConnection(url, user, pass);
3,测试存储过程
存储过程在jdbc调用的格式:{call 存储过程名(参数)}
运用存储过程可以节省流量,增快运行时数据交换的速度
public static void t() {
Emp emp = null;
try {
Connection conn = DBConnection.getConnection();
//创建调用存储过程的sql语句
String sql = "{call selEmp01}";
//创建调用存储过程的对象CallableStatement
CallableStatement call = conn.prepareCall(sql);
ResultSet rs = call.executeQuery();
while(rs.next()){
emp = new Emp();
emp.setEmpno(rs.getString("empno"));
emp.setEname(rs.getString("ename"));
emp.setJob(rs.getString("job"));
emp.setMgr(rs.getString("mgr"));
emp.setHiredate(rs.getString("hiredate"));
emp.setSal(rs.getDouble("sal"));
emp.setComm(rs.getDouble("comm"));
emp.setDeptno(rs.getInt("deptno"));
System.out.println(emp.toString());
}
DBConnection.closeAll(conn, call, rs);
} catch (SQLException e) {
e.printStackTrace();
}
}
4,测试分页的存储过程
java.sql.Types.INTEGER:代报参数注册
判断是否还有下一个结果集getMoreResults
存储过程解释:
go
create procedure emp01Fenye(
@tbName varchar(10),--表名
@currentPage int,--当前页
@pageSize int,--页的大小
@count int out--返回值,emp01的记录数
)
--select top 5 * from emp where empno not in (select top (@currentPage-1)*@pageSize empno from emp01 )分页的方法
as
declare
@sql varchar(200)
set @sql = 'select top '+convert(varchar(3),@pageSize)+' * from '+ @tbName+' where empno not in (select top '+CONVERT(varchar(3),((@currentPage-1)*@pageSize))+' empno from '+@tbName+' )'
exec (@sql)
select @count = COUNT(*) from EMP01;
select * from DEPT01;
public static void testemp01Fenye(){
try {
Connection conn = DBConnection.getConnection();
String sql = "{call emp01Fenye(?,?,?,?)}";
CallableStatement call = conn.prepareCall(sql);
//把存储过程的输入参数赋值
call.setString(1, "emp01");
call.setInt(2, 1);
call.setInt(3, 5);
//把存储过程需要输出的参数进行注册
call.registerOutParameter(4, java.sql.Types.INTEGER);
ResultSet rs = call.executeQuery();
Emp emp = null;
while(rs.next()){
emp = new Emp();
emp.setEmpno(rs.getString("empno"));
emp.setEname(rs.getString("ename"));
emp.setJob(rs.getString("job"));
emp.setMgr(rs.getString("mgr"));
emp.setHiredate(rs.getString("hiredate"));
emp.setSal(rs.getDouble("sal"));
emp.setComm(rs.getDouble("comm"));
emp.setDeptno(rs.getInt("deptno"));
System.out.println(emp.toString());
}
//判断是否还有下一个结果集getMoreResults
if(call.getMoreResults()){
//获取一个结果集
rs = call.getResultSet();
Dept d= null;
while(rs.next()){
d = new Dept();
d.setDeptno(rs.getInt("deptno"));
d.setDname(rs.getString("dname"));
d.setLoc(rs.getString("loc"));
System.out.println(d.toString());
}
}
//取出存储过程的返回值
int count = call.getInt(4);
//int count = call.getInt("count");
System.out.println(count);
DBConnection.closeAll(conn, call, rs);
} catch (SQLException e) {
e.printStackTrace();
}
}
5,添加日期,时间,布尔变量的方法
Connection conn = DBConnection.getConnection();
String sql = "insert into test values(?,?,?)";
PreparedStatement pre = conn.prepareStatement(sql);
pre.setInt(1, 1);
//创建一个日期类(import java.sql.Date;)的变量
java.util.Date da = new java.util.Date();
//da.getTime()获取当前的系统时间long类型
//创建一个(import java.sql.Date;)类的date变量d
Date d = new Date(da.getTime());
pre.setDate(2, d);
pre.setTime(3, new Time(da.getTime()));
pre.executeUpdate();
DBConnection.closeAll(conn, pre, null);