数据库连接的一般步骤

MySQL连接数据库的基本步骤:

/**
 * @category 数据库连接器
 * @function 负责数据库的链接、数据库资源的关闭处理
 */
package com.excelprocess.dbOper;


import java.sql.*;


public class DbConnector 
{

private String host=null;  //数据库主机
private String port=null;  //数据库连接端口
private String dbName=null;//数据库名称
private String user=null;  //登录账号
private String pw=null;  //登录密码
private Connection ct=null; //数据库连接对象

/**
* @category 无参构造函数
*/
public DbConnector()
{
this.host="";//默认数据库主机,服务器:
this.port="3306";//默认数据库连接端口
this.dbName="数据库名";//默认连接数据库
this.user="root";//登录账号
this.pw="root";//登录密码
}

/**
* @category 有参构造函数
* @param host 数据库主机
* @param port 数据库连接端口
* @param dbName 数据库名称
* @param user 登录账号
* @param password 登录密码
*/
public DbConnector(String host,String port,String dbName,String user,String password){
this.host = host;
this.port = port;
this.dbName = dbName;
this.user = user;
this.pw = password;
}


/**
* @category 数据库连接函数
* @return 连接成功返回非空Connection对象,连接失败返回空Connection对象
*/
public Connection getConnection()
{
String url="jdbc:mysql://"+this.host+":"+this.port+"/"+this.dbName+"?user="+this.user+"&password="+this.pw
+"&useUnicode=true&characterEncoding=utf-8";
try {
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
if(ct == null){//获取数据库连接对象
ct=DriverManager.getConnection(url);
System.out.println("数据库连接资源connection已打开!");
}
} catch (Exception e) {
e.printStackTrace();
}
return ct;
}

/**
* @category 数据库资源关闭函数
* @param ct Connection类的对象
* @param sm Statement类的对象
* @param rs ResultSet类的对象
* @return 数据库资源关闭成功返回true,否则返回false
*/
public boolean close_db(Connection ct,Statement sm,ResultSet rs)
{
boolean b=false;
try{
if(rs!=null){
rs.close();
rs=null;
}
if(sm!=null){
sm.close();
sm=null;
}
if(ct!=null){
ct.close();
ct=null;
}
if(rs==null && sm==null && ct==null)
{
b=true;
}
}catch(Exception ex){
ex.printStackTrace();
}
return b;
}

/**
* @category 数据库资源关闭函数
* @param ct Connection类的对象
* @param ps PreparedStatement类的对象
* @param rs ResultSet类的对象
* @return 数据库资源关闭成功返回true,否则返回false
*/
public boolean close_db(Connection ct,PreparedStatement ps,ResultSet rs)
{
boolean b=false;
try{
if(rs!=null){
rs.close();
rs=null;
}
if(ps!=null){
ps.close();
ps=null;
}
if(ct!=null){
ct.close();
ct=null;
}
if(rs==null && ps==null && ct==null)
{
b=true;
}
}catch(Exception ex){
ex.printStackTrace();
}
return b;
}

/**
* @category 数据库资源关闭函数
* @param ct Connection类的对象
* @param ps PreparedStatement类的对象
* @return 数据库资源关闭成功返回true,否则返回false
*/
public boolean close_db(Connection ct,PreparedStatement ps)
{
boolean b=false;
try{

if(ps!=null){
ps.close();
ps=null;
}
if(ct!=null){
ct.close();
ct=null;
}

if(ps==null && ct==null)
{
b=true;
}
}catch(Exception ex){
ex.printStackTrace();
}
return b;
}

}

JDBC访问数据库步骤 1. 新建java项目:JDBC,新建 class文件:TestJDBC 2. JDBC用到的类库基本都位于java.sql.*包中,程序中引入该包: Import java.sql.*; 3. 添加要用的数据库中的包,找到数据库中的Driver.class文件: 项目名上点右键,Build Path—Add External Archives… 构建路径----添加外部归档 加入mysql-connector-java-5.1.12 4. 从包中找到要用的驱动,展开包,从中找到Driver.class,编程,先把这个类的驱动new一个实例对象出来,告诉DriverManage,要连到哪种数据库上: 方法一:Class.forName(“com.mysql.jdbc.Driver”); Class: java.lang中的特殊类,类的装载器; forName: 会抛异常; com.mysql.jdbc.Driver中的Driver会new一个它的实例对象 方法二:new com.mysql.jdbc.Driver(); new出来后会自动向DriverManage注册。 5. 得到数据库连接: Connection conn=DriverManager.getConnection (数据库连接串,用户名,密码); 数据库连接串:“jdbc:mysql://localhost:3306/books” 用户名: “root” 密码: “111” 程序调试: import java.sql.*; public class TestJDBC { public static void main(String[] args)throws Exception { Class.forName("com.mysql.jdbc.Driver"); Connection conn=DriverManager.getConnection ("jdbc:mysql://localhost:3306/books?","root","111"); System.out.println("Connection Successful!"); } } * *对数据库表的操作通常有:executeQuery() executeUpdate() 6. (1)应用Statement接口 获取Statement对象,通过Statement对象执行SQL语句: Statement stmt=con.createStatement(); 执行SQL查询,返回给结果集对象: ResultSet rs=stmt. executeQuery(“select * from 表名”); 或 表名”+条件); 遍历访问数据表:while(rs.next()) 以各种类型显示输出:rs.get×××(“字段名”) (2)应用PreparedStatement接口 (p203) 执行数据更新executeUpdate():insert、update、delete SQL语句的创建:String sql=“sql命令”; 创建PreparedStatement的对象: pstmt=con. PrepareStatement(sql); 执行赋值操作(“?”占位符的用法): 执行:insert、update、delete result=pstmt. executeUpdate(); result类型为整型,返回一个整数,小于零操作没成功 7.关闭不再使用的 如:rs.close(); stmt.close(); con.close(); JDBC编程步骤总结: 1. Load the Driver:Class.forName(); 2. Connect the DateBase: DriveManager.getConnection() 3. Execute the SQL: (1) Connection.createStatement() Connection.prepareStatement(sql) (2)Statement.executeQuery() (3)Statement.executeUpdate() 4. Retrieve the result data: 循环取得结果while(rs.next()) 5. Show the result data:将遍历的结果记录显示出来 6.Close:结束关闭 //完善的JDBC程序 import java.sql.*; public class TestJDBC { public static void main(String[] args) { Connection conn=null; Statement stmt=null; ResultSet rs=null; try{ Class.forName("com.mysql.jdbc.Driver"); conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/books?","root","111"); System.out.println("Connection Successful!"); stmt=conn.createStatement(); rs=stmt.executeQuery("select * from titles"); while (rs.next()){ System.out.println(rs.getString("isbn")+" "+rs.getString("title")); } }catch(ClassNotFoundException e){ e.printStackTrace(); }catch(SQLException e){ e.printStackTrace(); }finally{ try{ if(rs!=null){ rs.close(); rs=null; } if(stmt!=null){ stmt.close(); stmt=null; } if(con!=null){ con.close(); con=null; } }catch(SQLException e){ e.printStackTrace(); } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值