Java基础--JDBC基础知识
class Dept
package fyl;
public class Dept {
private Integer deptno;
private String dname;
private String loc;
public Integer getDeptno() {
return deptno;
}
public void setDeptno(Integer deptno) {
this.deptno = deptno;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
}
Java查询MySQL数据库步骤
package fyl;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
public class ConnDemo {
public static void main(String[] args) {
try {
//jdbc访问数据库的步骤
//1:导入数据库的驱动包 mysql-connector-java-5.1.6-bin.jar
Class.forName("com.mysql.jdbc.Driver");//静态代码块实例化一个Driver对象
//2:建立连接对象
String url = "jdbc:mysql://192.168.3.159:3306/scott";
String user = "root";
String password = "root";
Connection connection = DriverManager.getConnection(url, user, password);
//3:准备sql指令
String sql = "SELECT deptno,dname,loc FROM dept; ";
//4:执行sql的预处理对象
PreparedStatement preparedStatement = connection.prepareStatement(sql);
//5:执行sql,返回结果集
ResultSet rs= preparedStatement.executeQuery();
//6:结果集的解析,放入map或bean中
List<Dept> depts =new ArrayList<Dept>();
while (rs.next()) {
// System.out.println(rs.getInt("deptno"));
// System.out.println(rs.getString("dname"));
// System.out.println(rs.getString("loc"));
// System.out.println("===================");
Dept dept = new Dept();
dept.setDeptno(rs.getInt("deptno"));
dept.setDname(rs.getString("dname"));
dept.setLoc(rs.getString("loc"));
depts.add(dept);
}
//7:关闭数据库连接
rs.close();
connection.close();
//8:测试
//9:返回给页面解析数据
for (Dept dept : depts) {
System.out.println("deptno="+dept.getDeptno()+" "+"dname="+dept.getDname()+" "+"loc="+dept.getLoc());
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
封装Util
package fyl;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
public class ConnUtil {
private static final String url = "jdbc:mysql://192.168.3.159:3306/scott";
private static final String user = "root";
private static final String password = "root";
/**
* 得到数据库连接
*/
public static Connection getConn() {
try {
//1:导入数据库的驱动包 mysql-connector-java-5.1.6-bin.jar
Class.forName("com.mysql.jdbc.Driver");//静态代码块实例化一个Driver对象
//2:建立连接对象
Connection connection = DriverManager.getConnection(url, user, password);
return connection;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 关闭数据库连接
*/
public static void CloseConn(Connection conn,PreparedStatement statement,ResultSet rs) {
try {
if(rs!=null) rs.close();
if(statement!=null) statement.close();
if(conn!=null) conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 查找数据
*/
public static List<Dept> findData() {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet rs = null;
List<Dept> depts = null;
Dept dept = null;
try {
connection = getConn();
//3:准备sql指令
String sql = "SELECT deptno,dname,loc FROM dept; ";
//4:执行sql的预处理对象
preparedStatement = connection.prepareStatement(sql);
//5:执行sql,返回结果集
rs= preparedStatement.executeQuery();
//6:结果集的解析,放入map或bean中
depts =new ArrayList<Dept>();
while (rs.next()) {
dept = new Dept();
dept.setDeptno(rs.getInt("deptno"));
dept.setDname(rs.getString("dname"));
dept.setLoc(rs.getString("loc"));
depts.add(dept);
}
return depts;
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
CloseConn(connection, preparedStatement, rs);
}
}
public static void main(String[] args) {
List<Dept> depts = findData();
for (Dept dept : depts) {
System.out.println("deptno="+dept.getDeptno()+" "+"dname="+dept.getDname()+" "+"loc="+dept.getLoc());
}
}
}