一、事前处理
1、简述
确保所用数据库可以连接。主要为mysql和SqlServer两种数据库。
1) SqlServer
java通过JDBC链接SQLServer2012
http://blog.csdn.net/stewen_001/article/details/19553173/
如何更改SQLServer 2008 登陆验证方式
http://blog.163.com/jackie_howe/blog/static/19949134720122261121214/
Win8怎么打开Telnet服务?Win8开启Telnet的步骤
http://www.win8.net/jiaocheng/20150927/2539.html
注:win10开启Telnet服务,会发现缺少一个Telnet服务端。从下面网址下载
telnetdSetup.exe 只能用一个月,破解版自行寻找
http://www.download25.com/install/telnet-server-for-windows-nt-2000-xp-2003.html
2) Mysql
相对简单,自行百度
实际原因为,我懒,不想找了
2、jar包
下载相应的jar包。将jar包添加相应的环境变量。或者将jar包加入相应的项目。
二、JDBC
1、包的导入
Import java.sql.*;
2、确认数据库连接
注:1、不同类型的数据库驱动和数据库链接地址不同
2、为方便以后叙述,将该以下代码成为一个类 DBhelper
public class DBHelper {
//SqlServer数据库驱动
private static String driverName="com.microsoft.sqlserver.jdbc.SQLServerDriver";
//SqlServer数据库链接地址
private static String dbURL="jdbc:sqlserver://localhost:1433;DatabaseName=?";//?为你的数据库名称
// mysql数据库的驱动
private static final String DRIVER = "com.mysql.jdbc.Driver";
//mysql数据库的连接地址
private static final String URL = "jdbc:mysql://localhost:3306/?";//?处为你的数据库名称
//数据库用户名名称
private static String userName="?";//? = 你的数据库用户名名称
//数据库密码
private static String userPwd="?";//? = 你的数据库密码
private static Connection conn = null;
//静态代码块加载数据库驱动
static{
try{
Class.forName(driverName);
}catch(ClassNotFoundException e){
e.printStackTrace();
}
}
// 通过单例模式返回数据库连接
public static Connection getConnection()throws SQLException {
if (conn == null) {
conn =DriverManager.getConnection(dbURL, userName, userPwd);
}
return conn;
}
//验证数据库是否连接
public static void main(String[] args) {
Connection conn;
try {
conn =DBHelper.getConnection();
if (conn != null) {
System.out.println("数据库连接成功");
} else {
System.out.println("数据库连接失败");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
三、执行SQL语句
1、Select
//此处返回一个对象
public Director getAnDirector(String num) {
Connectionconn = null; //数据库连接
PreparedStatementstmt = null; //预编译执行速度相对较快
ResultSetrs = null; //结果集
try{
conn= DBHelper.getConnection();//实现数据库连接
Stringsql = "select * from D where D#=" + num;
//输入SQL语句,通过+来实现数据的动态传入
stmt= conn.prepareStatement(sql); //预编译SQL语句
rs= stmt.executeQuery();
/*
用于产生单个结果集的语句,例如SELECT 语句。
被使用最多的执行 SQL 语句的方法是 executeQuery。
这个方法被用来执行 SELECT 语句,它几乎是使用最多的 SQL 语句
*/
if (rs.next()) {
Director director = new Director();//实例一个对象
director.setS_Dnum(rs.getString("D#"));
director.setS_Dname(rs.getString("DNAME"));
//rs.getString("D#")获取结果集中的值,通过SETTER函数复制
returndirector;//返回对象
}
returnnull;
}catch (SQLException e) {
e.printStackTrace();
returnnull;
}
finally{
//关闭结果集
if(rs != null) {
try{
rs.close();
rs = null; //赋值为null 相当于删除
}catch (SQLException e) {
e.printStackTrace();
}
}
if(stmt != null) {
try {
stmt.close();
stmt= null;
}catch (SQLException e) {
e.printStackTrace();
}
}
}
}
//此处返回一个对象
public Director getAnDirector(String num) {
Connectionconn = null; //数据库连接
PreparedStatementstmt = null; //预编译执行速度相对较快
ResultSetrs = null; //结果集
try{
conn= DBHelper.getConnection();//实现数据库连接
Stringsql = "select * from D where D#=" + num;
//输入SQL语句,通过+来实现数据的动态传入
stmt= conn.prepareStatement(sql); //预编译SQL语句
rs= stmt.executeQuery();
/*用于产生单个结果集的语句,例如SELECT 语句。
被使用最多的执行 SQL 语句的方法是 executeQuery。
这个方法被用来执行 SELECT 语句,它几乎是使用最多的 SQL 语句*/
if (rs.next()) {
Director director = new Director();//实例一个对象
director.setS_Dnum(rs.getString("D#"));
director.setS_Dname(rs.getString("DNAME"));
//rs.getString("D#")获取结果集中的值,通过SETTER函数复制
returndirector;//返回对象 } returnnull;
}catch (SQLException e) {
e.printStackTrace();
return null;
} finally{
//关闭结果集
if(rs != null) {
try{
rs.close();
rs = null; //赋值为null 相当于删除
}catch (SQLException e) {
e.printStackTrace();
}
}
if(stmt != null) {
try {
stmt.close();
stmt= null;
}catch (SQLException e) {
e.printStackTrace();
}
}
}
}
2、UPDATE INSERT DELETE
public int UpdataDirector(String num,Stringname,String pwd,String phone,String sex,String birth){
Connectionconn = null;
PreparedStatementstmt = null;
try{
conn= DBhelper.getConnection();
Stringsql = "UPDATE Director SET DNAME='"+name+"',DMES='"+pwd+"',DNUM='"+phone+"',DSEX='"+sex+"',DBIR='"+birth+"' WHERE D#='"+num+"';";
//SQL语句
stmt= conn.prepareStatement(sql);
intresult = stmt.executeUpdate();
/*方法executeUpdate
用于执行 INSERT、UPDATE 或 DELETE 语句以及 SQL DDL(数据定义语言)语句,例如CREATE TABLE 和 DROP TABLE。INSERT、UPDATE 或 DELETE 语句的效果是修改表中零行或多行中的一列或多列。executeUpdate 的返回值是一个整数,指示受影响的行数(即更新计数)。对于 CREATE TABLE 或 DROP TABLE 等不操作行的语句,executeUpdate 的返回值总为零。
使用executeUpdate方法是因为在createTableCoffees 中的 SQL 语句是 DDL (数据定义语言)语句。创建表,改变表,删除表都是 DDL 语句的例子,要用 executeUpdate 方法来执行。你也可以从它的名字里看出,方法 executeUpdate 也被用于执行更新表 SQL 语句。实际上,相对于创建表来说,executeUpdate 用于更新表的时间更多,因为表只需要创建一次,但经常被更新
*/
if(stmt != null) {
returnresult;
}
}catch (SQLException e) {
e.printStackTrace();
return0;
}finally {
if(stmt != null) {
try{
stmt.close();
stmt= null;
}catch (SQLException e) {
e.printStackTrace();
}
}
}
return0;
}
3、返回多个对象的值
//返回类型为Arraylist <>内类型
public ArrayList<EventStaff>StaffisAWhat(String num) {
Connectionconn = null;
PreparedStatementstmt = null;
ResultSetrs = null;
ArrayList<EventStaff>list = new ArrayList<EventStaff>(); // 储存查询的结果集
try{
conn= DBHelper.getConnection();
Stringsql = "select * from GS where S#=" + num;
stmt= conn.prepareStatement(sql);
rs= stmt.executeQuery();
while(rs.next()) { //通过循环输出结果集中的信息
EventStaff es = new EventStaff();
es.setS_ESsnum(rs.getString("S#"));
es.setS_ESgnum(rs.getString("G#"));
es.setIdentity(rs.getString("STATUS"));
list.add(es);//将此时的对象es添加到list中
}
returnlist;
}catch (SQLException e) {
e.printStackTrace();
returnnull;
}finally {
if(rs != null) {
try{
rs.close();
rs= null;
}catch (SQLException e) {
e.printStackTrace();
}
}
if(stmt != null) {
try{
stmt.close();
stmt= null;
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
如何输出LIST?
ArrayList<EventStaff> qw =a.StaffisAWhat("000001"); //实例list
if(qw != null && qw.size() > 0) { //判断是否有值
for(EventStaff aec : qw) { //循环输出
System.out.println(aec.getS_ESgnum());
}
}else {
System.out.println("error");
}