JDBC
Java DataBase Connectivity(Java语言连接数据库)
JDBC使用:
首先在项目上创建一个目录(Directory) 名称lib(固定的)
将下载好的jar包复制在lib目录下
右键jar包Add as Library
JDBC编程6步:
1.注册驱动
2.获取连接
3.获取数据库操作对象
4.执行SQL语句
5.处理查询结果集
6.释放资源
开发中三层架构:
代码:
jdbc.properties(配置文件)
driverClass=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/user?verifyServerCertificate=false&useSSL=false name=root password=123456
JDBCUtil(工具类)
//工具类
public class JDBCUtil {
static String driverClass=null;
static String url=null;
static String name=null;
static String password=null;
//读取配置文件 用io流
static{
//创建一个属性配置对象
Properties properties=new Properties();
InputStream is=null;
try {
//针对根目录E:\job\JDBC8.3
//is=new FileInputStream("jdbc.properties");
//针对src目录下读取文件
is=JDBCUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");
//加载
properties.load(is);
driverClass=properties.getProperty("driverClass");
url=properties.getProperty("url");
name=properties.getProperty("name");
password=properties.getProperty("password");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
//注册驱动建立连接
public static Connection getConn(){
Connection conn=null;
try {
Class.forName(driverClass);
conn= DriverManager.getConnection(url,name,password);
} catch (Exception e) {
throw new RuntimeException(e);
}
return conn;
}
//关闭
private static void closeRs(ResultSet rs){
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}finally{
rs=null;
}
}
}
private static void closeSt(PreparedStatement ps){
if(ps!=null){
try {
ps.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}finally{
ps=null;
}
}
}
private static void closeConn(Connection conn){
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}finally{
conn=null;
}
}
}
public static void release(ResultSet rs,PreparedStatement ps,Connection conn){
closeRs(rs);
closeSt(ps);
closeConn(conn);
}
public static void release(PreparedStatement ps,Connection conn){
closeSt(ps);
closeConn(conn);
}
}
接口(UserDao)
public interface UserDao {
//查询所有
void findAll();
//登录
void login(String username,String pwd);
//新增
void insert(String username,String pwd);
//修改
void update(String username1,String username2);
//删除
void delete(String name);
}
实现接口(UserDaoImpl)
public class UserDaoImpl implements UserDao {
Connection conn=null;
//Statement st=null;
PreparedStatement ps=null;
ResultSet rs=null;
@Override
public void findAll() {
try {
conn= JDBCUtil.getConn();
String sql="select * from t_user";
ps=conn.prepareStatement(sql);
rs =ps.executeQuery();
while(rs.next()){
int id=rs.getInt("id");
String username=rs.getNString("username");
String pwd=rs.getNString("pwd");
System.out.println("id:"+id+"\tusername:"+username+"\tpwd:"+pwd);
}
} catch (SQLException e) {
throw new RuntimeException(e);
}finally{
JDBCUtil.release(rs,ps,conn);
}
}
/* @Override
public void login(String username,String pwd) {
try {
conn=JDBCUtil.getConn();
st=conn.createStatement();
String sql="select * from t_user where username='"+username+"' and pwd='"+pwd+"' ";
ResultSet rs = st.executeQuery(sql);
if(rs.next()){
System.out.println("登录成功");
}else{
System.out.println("登录失败");
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}*/
@Override
public void login(String username,String pwd) {
try {
conn=JDBCUtil.getConn();
//st=conn.createStatement();
String sql="select * from t_user where username=? and pwd=? ";
//对sql语句进行语法检查 不管传进来什么都看作字符串 ? 从1开始 索引1开始
PreparedStatement ps=conn.prepareStatement(sql);
ps.setString(1,username);
ps.setString(2,pwd);
rs = ps.executeQuery();//因为已经预处理过sql所以不放参数
if(rs.next()){
System.out.println("登录成功");
}else{
System.out.println("登录失败");
}
} catch (SQLException e) {
throw new RuntimeException(e);
}finally{
JDBCUtil.release(rs, ps, conn);
}
}
@Override
public void insert(String username,String pwd) {
try {
conn=JDBCUtil.getConn();
String sql="insert into t_user(username,pwd) values(?,?)";
ps=conn.prepareStatement(sql);
ps.setString(1,username);
ps.setString(2,pwd);
int i=ps.executeUpdate();
if(i>0){
System.out.println("新增成功");
}else{
System.out.println("新增失败");
}
} catch (SQLException e) {
throw new RuntimeException(e);
}finally{
JDBCUtil.release( ps, conn);
}
}
@Override
public void update(String username1,String username2) {
try {
conn=JDBCUtil.getConn();
String sql="update t_user set username=? where username=? ";
ps=conn.prepareStatement(sql);
ps.setString(1,username1);
ps.setString(2,username2);
int i=ps.executeUpdate();
if(i>0){
System.out.println("修改成功");
}else{
System.out.println("修改失败");
}
} catch (SQLException e) {
throw new RuntimeException(e);
}finally{
JDBCUtil.release( ps, conn);
}
}
@Override
public void delete(String name) {
try {
conn=JDBCUtil.getConn();
String sql="delete from t_user where username=?";
ps=conn.prepareStatement(sql);
ps.setString(1,name);
int i=ps.executeUpdate();
if(i>0){
System.out.println("删除成功");
}else{
System.out.println("删除失败");
}
} catch (SQLException e) {
throw new RuntimeException(e);
}finally{
JDBCUtil.release( ps, conn);
}
}
}
测试类(TestUserDaoImpl)
public class UserDaoImplTest {
@Test
public void testFindAll(){
UserDao u=new UserDaoImpl();
u.findAll();
}
@Test
public void testLogin(){
UserDao u=new UserDaoImpl();
u.login("张三","0128");
}
@Test
public void testInsert(){
UserDao u=new UserDaoImpl();
u.insert("张三","31423");
}
@Test
public void testUpdate(){
UserDao u=new UserDaoImpl();
u.update("张三丰","张三");
}
@Test
public void testDelete(){
UserDao u=new UserDaoImpl();
u.delete("张三丰");
}
}
HTML
文本标签
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>标题</title>
</head>
<body>
html静态页面
<!-- 文本标签 -->
<strong>加粗</strong>
<em>倾斜字体</em>
<small>变小</small>
<!-- 标题标签 <h1></h1> h1~h6 字体越来越小 -->
<h1>html静态页面 标题1</h1>
<h6>html静态页面 标题6</h6>
<!-- 段落标签 -->
<p>段落标签</p>
<b>换段</b>
<br>换行
<!-- 水平线标签 -->
<hr>水平线
<hr color="read" size="7"><!-- color 颜色 size 尺寸 -->
<!-- 注释和特殊符号标签 -->
<!-- 空格 -->
你 好
<!-- 大于号> > -->
2>1
<!-- 小于号< < -->
1<2
<!-- 引号"" " -->
"你好"
<!-- 版权符号@ © -->
©木舟
</body>
</html>
列表标签
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>列表</title>
</head>
<body>
<!-- 无序列表 -->
<ul>
<li>生活家居</li>
<li>数码产品</li>
<li>彩妆女装</li>
</ul>
<!-- 方块 -->
<ul type="square">
<li>生活家居</li>
<li>数码产品</li>
<li>彩妆女装</li>
</ul>
<!-- 空心圆 -->
<ul type="circle">
<li>生活家居</li>
<li>数码产品</li>
<li>彩妆女装</li>
</ul>
<!-- 有序列表 -->
<ol><!-- 默认为 1 2 3 阿拉伯数字 -->
<li>生活家居</li>
<li>数码产品</li>
<li>彩妆女装</li>
</ol>
<ol type="A" start="5"><!-- start 指定从那个位置开始 -->
<li>生活家居</li>
<li>数码产品</li>
<li>彩妆女装</li>
</ol>
<ol type="I">
<li>生活家居</li>
<li>数码产品</li>
<li>彩妆女装</li>
</ol>
<!-- 自定义列表 -->
<dl>
<dt>所属学院</dt>
<dd>计算机应用</dd>
<dt>所属专业</dt>
<dd>计算机软件工程</dd>
</dl>
</body>
</html>
表格标签
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>表格</title>
</head>
<body>
<!-- 基本结构:行 列 单元格 -->
<table border="1" cellspacing="5" cellpadding="10" align="left">
<!-- border 表格的边框 -->
<!-- cellspacing外边距单元格到边框的距离 -->
<!-- cellpadding内边距文本到表格的距离-->
<!-- align让表格居中或者靠左靠右 left center right -->
<caption>成绩单</caption><!-- caption 表格的标题 -->
<tr><!-- tr 行 -->
<td>语文</td><!-- td 列 -->
<th>98</th><!-- th加粗 -->
</tr>
<tr>
<td>数学</td>
<td>99</td>
</tr>
</table>
<table border="1">
<caption>成绩单</caption>
<tr>
<td colspan="2">语文</td><!-- colspan 跨列 -->
<!-- <th>98</th> -->
</tr>
<tr>
<td>数学</td>
<td>99</td>
</tr>
</table>
<table border="1">
<caption>成绩单</caption>
<tr>
<td>语文</td>
<!-- <td>98</td> -->
</tr>
<tr>
<td>数学</td>
<td rowspan="2">99</td><!-- rowspan 跨行 -->
</tr>
</table>
</body>
</html>
页面排版框架
框架不能写在<body></body>之间
横向分割,纵向分割
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<!-- <body>
框架不能写在body之间
</body> -->
<frameset cols="25%,50%,*"><!-- cols纵向分割 rows横向分割 -->
<frame src="first.html"/>
<frame src="second.html"/>
<frame src="third.html"/>
</frameset>
</html>
跳转
<a href="要跳转的页面.html" target="_blank">点击跳转</a><!-- target 跳转后新建一个页面 -->
表单
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>表单</title>
</head>
<body>
<form action="success.html" method="post"><!-- method 不建议使用get 不安全 -->
账号:<input type="text" name="uname" value=""/><br/>
密码:<input type="password" name="pwd" value=""/><br/>
性别<input type="radio" name="gender" value="男" checked="checked"/>男<!-- radio 单选框 checked 默认为男-->
<input type="radio" name="gender" value="女"/>女<br/><!-- name的名字必须一致 不然就会出现可以同时选择的情况 -->
爱好:<input type="checkbox" checked="checked" />篮球
<input type="checkbox"/>足球
<input type="checkbox"/>羽毛球
<input type="checkbox"/>乒乓球
<input type="checkbox"/>台球<br/>
年份:
<select>
<option>1990</option>
<option selected="selected">请选择</option> <!-- selected 默认-->
<option>1991</option>
<option>1992</option>
<option>1993</option>
<option>1994</option>
<br/>
<input type="submit" value="登录"/>
<input type="reset" value="重置"/>
<input type="button" value="普通按钮" disabled="disabled"/><!-- 通过js添加事件 -->
<!-- disabled禁用 -->
<input type="image" src="../images/无标题.png"/><!-- 图片按钮 -->
<textarea cols="10" rows="10" readonly="readonly">免责协议......</textarea><!-- 多行文本框 -->
<!-- readonly 只读 --><br/>
<input type="hidden" name="id" value=""/><!-- 隐藏域 --><br/>
<input type="file"/><!-- 文件域 -->
</select>
</form>
</body>
</html>