目录
一 设计理念
二 官网下载
三 代码步骤
一 项目与模块引用(导入)jar包
二 具体操作
1 前提:需要sqlsever已经建表且有测试数据
2 具体操作
JDBC(Java Database Connectivity )
Java连接数据库执行SQL(DML,DDL)语句的技术
一 设计理念
二 JDBC的jar包官网下载
三 代码步骤
一 设计理念
定义了一些接口(java.sql包中),各个数据库厂商实现接口 Driver,Connection,Statement,ResultSet
二 官网下载
搜索:sql server 2005 jdbc driver
(mysql同理)
三 代码步骤
一 项目与模块引用(导入)jar包
以IDEA为例:①创建Modules: 02_JDBCTest(随你)
② 给project添加jdbc.jar : File ->project structure ->Labraries的右边框 中点击+号 -> java 复制刚刚下载好的sqljdbc4.jar的路径然后点击apply
③给Modules 添加jdbc.jar :在project structure 中点击Modules 再点击 +号,选中sqljdbc4 -> ok
二 具体操作
1 前提:需要sqlsever已经建表且有测试数据
use master
if exists(select * from sysdatabases where name = 'PAS')
drop database PAS
--Performance AnalysisSystem System
go
create database PAS
go
use PAS
create table lesson (
lesId int identity not null,
lesName varchar(20) not null,
context varchar(20) not null,
score int not null,
hours int not null
);
alter table lesson add constraint PK_lesId
primary key (lesId)
go
use master if exists(select * from sysdatabases where name = 'PAS') drop database PAS --Performance AnalysisSystem System go create database PAS go use PAS create table lesson ( lesId int identity not null, lesName varchar(20) not null, context varchar(20) not null, score int not null, hours int not null ); alter table lesson add constraint PK_lesId primary key (lesId) go
insert into lesson(lesname,context, score, hours) values ('设计模式','编程孙子兵法',4,56)
update lesson set lesName= '设计模式', context = '编程孙子兵法', score=5, hours = 30 where lesId = 10
use pas
select * from lesson
delete from lesson where lesId = 10
2 具体操作
①加载驱动程序Class.forName("驱动程序类包名+类名")
a) 静态方法,类名.方法名
b) 抛出一个编译时异常ClassNotFoundException(Alt + Enter错误代码提示)
c) 驱动程序类Implements java.sql.driver接口(Ctrl + n打开新类 Ctrl +Shift +F4 关闭当前类视窗)
②建立连接Connection DriverManger.getConnetion(url)
a)Connection con = DriverManger.getConnetion(url, userName, pwd) //sqlsever
b)抛出一个编译时异常SQLException(调用java.sql)
c)url统一资源定位符 jdbc:type://ip:port;databaseName=daName
③创建Statement对象 con.createStatement()
a)执行sql语句
int [语句影响行数]executeUpdate(sql[insert,update,delete])
b)ResultSet executeQuery(sql) select
④关闭资源
a) 关闭资源顺序与创建资源顺序相反
b) 当一个对象没有指向它的任何引用时,表示是垃圾资源
package com.wangtan.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class LessonDAO {
public void save(){
String sql = "insert into lesson(lesname,context, score, hours) values ('设计模式','编程孙子兵法',4,56)";
Connection con = null;
Statement sta = null;
//1 加载驱动程序Class.forName(驱动程序类包名+类名)
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url = "jdbc:sqlserver://localhost:1433;databaseName=pas";
//抛出一个编译时异常SQLException(调用java.sql包中的类的方法都会抛出)
//url:统一资源定位符 jdbc:type://ip:port;databaseName=daName
//2 得到链接
con = DriverManager.getConnection(url, "sa","sa");
//3 创建Statement
sta = con.createStatement();
//4 执行sql
int rows = sta.executeUpdate(sql);
if(rows == 1){
System.out.println("insert success!");
} else {
System.out.println("insert error!");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
finally {
//调用java.sql包中的类的方法都会抛出编译时异常SQLException
try {
if(sta != null){
sta.close();
sta = null;
}
if (con != null){
con.close();
con = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}



