package Util;
2
3 import java.io.FileInputStream;
4 import java.io.FileNotFoundException;
5 import java.io.IOException;
6 import java.sql.Connection;
7 import java.sql.DriverManager;
8 import java.sql.PreparedStatement;
9 import java.sql.ResultSet;
10 import java.sql.SQLException;
11 import java.sql.Timestamp;
12 import java.util.Properties;
13
14 public class JdbcUtil {
15
16 static Properties pros=
null;
17 static Connection con=
null;
18 static ResultSet rs=
null;
19 static PreparedStatement psSQL=
null;
20
21 static {
22 pros =
new Properties();
23 try {
24 pros.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("db.properties"
));
25 }
catch (IOException e) {
26 e.printStackTrace();
27 }
28 }
29
30 private JdbcUtil() {
31
32 }
33
34 public static Connection getMySQLConnection() {
35 try {
36 Class.forName(pros.getProperty("conPackage"
));
37 con=DriverManager.getConnection(pros.getProperty("url"),pros.getProperty("user"),pros.getProperty("pwd"
));
38 return con;
39 }
catch (SQLException e) {
40 e.printStackTrace();
41 return null;
42 }
catch (ClassNotFoundException e) {
43 e.printStackTrace();
44 return null;
45 }
46 }
47
48 public static boolean insertuser(String phone,String pwd ){
49 try {
50 psSQL=con.prepareStatement("SELECT * FROM USER WHERE PHONE = ?"
);
51 psSQL.setObject(1
, phone);
52 rs=
psSQL.executeQuery();
53 if(rs.next()) {
54 return false;
55 }
else {
56 psSQL=con.prepareStatement("INSERT INTO USER (PHONE,PWD) VALUE (?,?)"
);
57 psSQL.setString(1
, phone);
58 psSQL.setString(2
, pwd);
59 psSQL.execute();
60 return true;
61 }
62 }
catch (SQLException e) {
63 e.printStackTrace();
64 return false;
65 }
66 }
67
68
69 public static boolean register(String phone ,String pwd) {
70 try {
71 PreparedStatement psSQL =con.prepareStatement("SELECT * FROM USER WHERE PHONE = ? AND PWD= ?"
);
72 psSQL.setString(1
, phone);
73 psSQL.setString(2
, pwd);
74 rs=
psSQL.executeQuery();
75 if(rs.next()) {
76 return true;
77 }
else {
78 return false;
79 }
80 }
catch (SQLException e) {
81 e.printStackTrace();
82 return false;
83 }
84 }
85
86 public static boolean updateDetail(String phone,String nickname,String number,
char sex,String name,String path) {
87 try {
88 psSQL =con.prepareStatement("UPDATE USER SET NICKNAME=? ,NUMBER =? ,SEX= ?, NAME=? ,PATH=? WHERE PHONE =?"
);
89 psSQL.setString(1
, nickname);
90 psSQL.setString(2
, number);
91 byte intsex;
92 if(sex==‘男‘
) {
93 intsex=1
;
94 }
else {
95 intsex=0
;
96 }
97 psSQL.setObject(3
, intsex);
98 psSQL.setString(4
, name);
99 psSQL.setString(5
, path);
100 psSQL.setString(6
, phone);
101 psSQL.execute();
102 return true;
103 }
catch (SQLException e) {
104 e.printStackTrace();
105 return false;
106 }
107 }
108
109 public static boolean addIcon(String phone,String imagePath) {
110 try {
111 psSQL=con.prepareStatement("update user set icon=? where phone=?"
);
112 psSQL.setBlob(1,
new FileInputStream(imagePath));
113 psSQL.setString(2
, phone);
114 psSQL.execute();
115 return true;
116 }
catch (FileNotFoundException e) {
117 e.printStackTrace();
118 return false;
119 }
catch (SQLException e) {
120 e.printStackTrace();
121 return false;
122 }
123 }
124
125 public static boolean addDeal(String phone ,
long time,String address,String goClass,String why,String teacher) {
126 try {
127 psSQL =con.prepareStatement("INSERT INTO DAIKE (PHONE,TIME,ADDRESS,CLASS,WHY,TEACHER) VALUE (?,?,?,?,?,?)"
);
128 psSQL.setString(1
, phone);
129 Timestamp timeStamp=
new Timestamp(time);
130 psSQL.setTimestamp(2
, timeStamp);
131 psSQL.setString(3
,address);
132 psSQL.setString(4
, goClass);
133 psSQL.setString(5
, why);
134 psSQL.setString(6
, teacher);
135 psSQL.execute();
136 return true;
137 }
catch (SQLException e) {
138 e.printStackTrace();
139 return false;
140 }
141 }
142
143 public static ResultSet getDealList(String phone) {
144 try {
145 psSQL=con.prepareStatement("select * from daike where phone =?"
);
146 psSQL.setString(1
, phone);
147 return psSQL.executeQuery();
148 }
catch (SQLException e) {
149 e.printStackTrace();
150 return null;
151 }
152 }
153
154 public static void close() {
155 try {
156 if(rs!=
null) {
157 rs.close();
158 }
159 }
catch (SQLException e) {
160 e.printStackTrace();
161 }
162 try {
163 if(psSQL!=
null) {
164 psSQL.close();
165 }
166 }
catch (SQLException e) {
167 e.printStackTrace();
168 }
169 try {
170 if(con!=
null) {
171 con.close();
172 }
173 }
catch (SQLException e) {
174 e.printStackTrace();
175 }
176 }
177
178
179 }
JdbcUtil
1 url:jdbc:mysql://localhost:3306/didi?useUnicode=true&characterEncoding=utf-8&useSSL=false
2 user:root
3 pwd:123456
4 conPackage:com.mysql.jdbc.Driver
5 userTable:user
db.properties
dididaikeJdbcUtil备份
标签:phone private cti ade ext exce ddr src nic