切换主题
Java数据库操作之JDBC
Connect.java
java
package com.pcf.lab_jsp_3;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Connect {
private static final String userName = "root";
private static final String password = "123456";
private static final String dbName = "lab_jsp_3";
private static final String driver = "com.mysql.jdbc.Driver";
private static final String url = "jdbc:mysql://127.0.0.1:3306/" + dbName + "?useSSL=false";
public static Connection getConnection() throws ClassNotFoundException, SQLException {
Class.forName(driver);
return DriverManager.getConnection(url, userName, password);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
JDBCUtils.java
java
/*
* JDBCUtils
*
* $Id$
* $HeadURL$
*/
import java.lang.reflect.InvocationTargetException;
import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
/**
* JDBC Connection and Statement utility functions.
*
* @author jmfee
*/
public class JDBCUtils {
/**
* Mysql Driver.
*/
public static final String MYSQL_DRIVER_CLASSNAME = "com.mysql.jdbc.Driver";
/**
* SQLite Driver.
*/
public static final String SQLITE_DRIVER_CLASSNAME = "org.sqlite.JDBC";
/**
* Create a new JDBC Connection.
*
* @param driver driver class name.
* @param url driver specific url.
* @return Connection to database.
* @throws ClassNotFoundException if driver class is not found.
* @throws IllegalAccessException if driver empty constructor is not public.
* @throws InstantiationException if an exception occurs while instantiating driver.
* @throws InvocationTargetException if an exception occurs with invoked method
* @throws NoSuchMethodException if method cannot be found
* @throws SQLException if an error occurs while making connection.
*/
public static Connection getConnection(final String driver, final String url)
throws ClassNotFoundException, IllegalAccessException,
InstantiationException, InvocationTargetException,
NoSuchMethodException, SQLException {
// create driver class, which registers with DriverManager
Class.forName(driver).getConstructor().newInstance();
// request connection from DriverManager
return DriverManager.getConnection(url);
}
/**
* Set a JDBC prepared statement parameter.
* <p>
* Either calls statement.setNull if object is null, or sets the appropriate
* type based on the object. If the object is not null, type is ignored.
*
* @param statement statement with parameters to set.
* @param index index of parameter being set.
* @param object value of parameter being set.
* @param type java.sql.Types constant for column type.
* @throws SQLException if an error occurs while making connection.
*/
public static void setParameter(final PreparedStatement statement,
final int index, final Object object, final int type)
throws SQLException {
if (object == null) {
statement.setNull(index, type);
} else if (object instanceof Boolean) {
statement.setBoolean(index, (Boolean) object);
} else if (object instanceof Byte) {
statement.setByte(index, (Byte) object);
} else if (object instanceof Character) {
statement.setString(index, ((Character) object).toString());
} else if (object instanceof Double) {
statement.setDouble(index, (Double) object);
} else if (object instanceof Float) {
statement.setFloat(index, (Float) object);
} else if (object instanceof Integer) {
statement.setInt(index, (Integer) object);
} else if (object instanceof Long) {
statement.setLong(index, (Long) object);
} else if (object instanceof Short) {
statement.setShort(index, (Short) object);
} else if (object instanceof String) {
statement.setString(index, (String) object);
} else {
statement.setObject(index, object, type);
System.err.printf(
"Unsupported object type (%s): index=%d, value=%s\n",
object.getClass().getName(), index, object.toString());
}
}
/**
* Get a mysql connection from a URL.
* <p>
* Calls getConnection(MYSQL_DRIVER_CLASSNAME, url).
*
* @param url a Mysql URL.
* @return a Connection to a Mysql database.
* @throws SQLException if an error occurs while making connection.
* @throws ClassNotFoundException if driver class is not found.
* @throws IllegalAccessException if driver empty constructor is not public.
* @throws InstantiationException if an exception occurs while instantiating driver.
* @throws InvocationTargetException if an exception occurs with invoked method
* @throws NoSuchMethodException if method cannot be found
*/
public static Connection getMysqlConnection(final String url)
throws SQLException, ClassNotFoundException,
IllegalAccessException, InstantiationException,
InvocationTargetException, NoSuchMethodException {
return getConnection(MYSQL_DRIVER_CLASSNAME, url);
}
/**
* Get a sqlite connection from a file.
* <p>
* Builds a sqlite file url and calls getSqliteConnection(url).
*
* @param file sqlite database file.
* @return connection to sqlite database file.
* @throws SQLException if an error occurs while making connection.
* @throws ClassNotFoundException if driver class is not found.
* @throws IllegalAccessException if driver empty constructor is not public.
* @throws InstantiationException if an exception occurs while instantiating driver.
* @throws InvocationTargetException if an exception occurs with invoked method
* @throws NoSuchMethodException if method cannot be found
*/
public static Connection getSqliteConnection(final File file)
throws SQLException, ClassNotFoundException,
IllegalAccessException, InstantiationException,
InvocationTargetException, NoSuchMethodException {
String sqliteFileURL = "jdbc:sqlite:" + file.getAbsolutePath();
return getSqliteConnection(sqliteFileURL);
}
/**
* Get a sqlite connection from a URL.
* <p>
* Calls getConnection(SQLITE_DRIVER_CLASSNAME, url).
*
* @param url sqlite database URL.
* @return a Connection to a sqlite database.
* @throws SQLException if an error occurs while making connection.
* @throws ClassNotFoundException if driver class is not found.
* @throws IllegalAccessException if driver empty constructor is not public.
* @throws InstantiationException if an exception occurs while instantiating driver.
* @throws InvocationTargetException if an exception occurs with invoked method
* @throws NoSuchMethodException if method cannot be found
*/
public static Connection getSqliteConnection(final String url)
throws SQLException, ClassNotFoundException,
IllegalAccessException, InstantiationException,
InvocationTargetException, NoSuchMethodException {
return getConnection(SQLITE_DRIVER_CLASSNAME, url);
}
}
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165