博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
数据库连接学习--简单的通讯录
阅读量:6899 次
发布时间:2019-06-27

本文共 2017 字,大约阅读时间需要 6 分钟。

为了做毕业设计,学习了Java,然后就要连接数据库,为了连接数据库就学习做了一个简单的小项目,通讯录(现在只有添加的功能),成功连接数据库

首先看看我的WEB首页吧:

比较简单,然后是填加联系人页面

我的数据库连接的代码先抛出来,毕竟这是我做通讯录学习的重点,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package 
s2.jsp.zhangxiao.dao;
 
import 
java.sql.PreparedStatement;
import 
java.sql.Connection;
import 
java.sql.ResultSet;
import 
java.sql.DriverManager;
import 
java.sql.SQLException;
 
public 
class 
AddressBase {
 
   
    
// 定义数据库的用户名
    
private 
final 
String DBNAME = 
"root"
;
    
// 定义数据库的密码
    
private 
final 
String DBPASS = 
"0"
;
    
// 定义数据库的驱动信息
    
private 
final 
String DRIVER = 
"com.mysql.jdbc.Driver"
;
    
// 定义访问数据库的地址
    
private 
final 
String URL = 
"jdbc:mysql://localhost:3306/addressdb"
;
   
  
public 
Connection getConnection(){
    
Connection con=
null
;
    
try 
{
        
Class.forName(DRIVER);
        
con=DriverManager.getConnection(URL, DBNAME, DBPASS);
    
catch 
(ClassNotFoundException e) {
        
System.out.println(
"驱动异常"
);
        
e.printStackTrace();
    
catch 
(SQLException e) {
    
System.out.println(
"数据库异常"
);
        
e.printStackTrace();
    
}
    
return 
con;
       
  
}
  
public 
void 
closeAll(Connection con,PreparedStatement past,ResultSet rs){
      
try 
{
        
rs.close();
    
catch 
(SQLException e) {
        
// TODO Auto-generated catch block
        
e.printStackTrace();
    
}
      
try 
{
        
past.close();
    
catch 
(SQLException e) {
        
// TODO Auto-generated catch block
        
e.printStackTrace();
    
}
      
try 
{
        
con.close();
    
catch 
(SQLException e) {
        
// TODO Auto-generated catch block
        
e.printStackTrace();
    
}
  
}
  
public 
int 
update(String sql,String[]getValues){
    
int 
i=
0
;
    
Connection con=
null
;
    
PreparedStatement past=
null
;
    
ResultSet rs=
null
;
    
con=getConnection();
    
try 
{
        
past=con.prepareStatement(sql);
        
if 
(getValues!=
null
) {
            
for 
(
int 
j = 
0
; j < getValues.length; j++) {
                
past.setString(j+
1
, getValues[j]);
            
}
        
}
        
i=past.executeUpdate();
    
catch 
(SQLException e) {
        
// TODO Auto-generated catch block
        
e.printStackTrace();
    
}
    
return 
i;
       
  
}
}
本文转自 七十七快 51CTO博客,原文链接:http://blog.51cto.com/10324228/1926791

转载地址:http://btcdl.baihongyu.com/

你可能感兴趣的文章
阿里云授权服务中心解答阿里云备案相关疑问
查看>>
一些收集的MikroTik RouterOS破解版虚拟机VMware
查看>>
wordpress无法更新为最新版本
查看>>
爬虫代码编写中会遇到的字符处理的坑
查看>>
SSM-Spring-09:Spring中jdk动态代理
查看>>
我为NET狂官方面试题-数据库篇
查看>>
HBase集群安装
查看>>
Ubuntu终端字体大小设置快捷键
查看>>
[20180625]函数与标量子查询13(补充)
查看>>
Python面试问题整理(附答案)
查看>>
设计模式—装饰模式的C++实现
查看>>
MySQL:Innodb page clean 线程 (二) 解析
查看>>
Android CircularFloatingActionMenu:作为系统级按钮悬浮桌面弹出菜单使用(3)
查看>>
正确配置Kubelet可一定程度防止K8S集群雪崩
查看>>
Content Aware ABR技术
查看>>
Spring系列之Spring框架和SpringAOP集成过程分析(十一)
查看>>
云数据库产品月刊·5月刊
查看>>
50行代码的MVVM,感受闭包的艺术
查看>>
Android第三方开源图片裁剪截取:cropper
查看>>
直播转点播实践
查看>>