1、直接在main方法中一步一步连接数据库
具体的每一步的解析都已经写在注释里了,注意这里获取Connection对象的方法已经被抽象出来专门放在一个类里面了
public class JDBCDemo {
public static void main(String[] args) {
try {
//注册驱动
Driver drive= new Driver();
DriverManager.registerDriver(drive);
//获取数据库连接对象
String url="jdbc:mysql://localhost:3306/day04";
Connection conn=(Connection) DriverManager.getConnection(url,"root","root");
//获取实际的SQL语句执行者对象
Statement st = (Statement) conn.createStatement();
//对执行者对象进行操作!然后放入结果集
ResultSet rs=st.executeQuery("select * from category");
//对结果集进行遍历
while (rs.next())
{
//可以单独获得结果集中的一个字段
Object cid = rs.getObject("cid");
Object cname = rs.getObject("cname");
System.out.println(cid+"\t"+cname);
}
//连接对象、结果集、执行者对象都要进行资源释放~~
rs.close();
st.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
专门用于管理Connection对象的一个类:
public class JDBCUtils {
private static String driverName="com.mysql.jdbc.Driver";
private static String url="jdbc:mysql://localhost:3306/day04";
private static String userName="root";
private static String password="root";
//静态代码块
static{
try {
Class.forName(driverName);
} catch (ClassNotFoundException e) {
//驱动加载失败
System.out.println("驱动加载失败,请检查驱动包!");
throw new RuntimeException("驱动加载失败,请检查驱动包!");
}
}
public static java.sql.Connection getConnection() throws Exception{
Class.forName(driverName);
java.sql.Connection connection =DriverManager.getConnection(url,userName,password);
return connection;
}
public static void closeAll(java.sql.Connection connection,Statement st,ResultSet rs)
{
if(connection!=null)
{
try {
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(st!=null)
{
try {
st.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(rs!=null)
{
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
2、对常用的数据库操作方法进行封装
public static void insert(){
Connection connection=null;
Statement statement=null;
try {
connection=JDBCUtils.getConnection();
statement = connection.createStatement();
int rows = statement.executeUpdate("insert into category (cname) values ('饮料')");
System.out.println("成功插入"+rows+"行");
} catch (Exception e) {
// TODO: handle exception
}finally {
JDBCUtils.closeAll(connection, statement, null);
}
}
public static void delete(){
Connection connection=null;
Statement statement=null;
ResultSet ra=null;
try {
connection=JDBCUtils.getConnection();
statement = connection.createStatement();
int rows=statement.executeUpdate("delete from category where cname='饮料'");
System.out.println("成功删除"+rows+"行");
} catch (Exception e) {
// TODO: handle exception
}finally {
JDBCUtils.closeAll(connection, statement, ra);
}
}
public static void update(){
Connection connection=null;
Statement statement=null;
ResultSet ra=null;
try {
} catch (Exception e) {
// TODO: handle exception
}finally {
JDBCUtils.closeAll(connection, statement, ra);
}
}
public static void query(){
Connection connection=null;
Statement statement=null;
ResultSet ra=null;
try {
} catch (Exception e) {
// TODO: handle exception
}finally {
JDBCUtils.closeAll(connection, statement, ra);
}
}
网友评论