|
我在MySQL中用一个表创建了一个数据库:2 D0 n' d5 B& ~0 E7 I3 {+ m! a
CREATE DATABASE iac_enrollment_system;
0 K g/ T6 I0 hUSE iac_enrollment_system;
5 G# j, ]6 i. W h* G; {CREATE TABLE course(! ?7 T- T: u0 ?8 H2 N( H* T
course_code CHAR(7)," O' y# U9 y, [/ @
course_desc VARCHAR(255) NOT NULL,* _, T. y, v3 e- q) ]2 e w
course_chair VARCHAR(255),/ R1 y: }) H6 ]5 t
PRIMARY KEY(course_code)7 n6 { z, ^" h) Y8 w5 g- u
);) |. z8 p5 C- P$ |/ K# V ~
我尝试使用Java插入记录:9 H* E/ \2 N; n9 S
// STEP 1: Import required packages
& q7 C4 R1 W, |) jimport java.sql.*;1 e8 i# R. _% ^$ c' u' \# S8 d
import java.util.*;
, k" }9 M2 x! S) Bpublic class SQLInsert {
" ?; x8 z V4 V$ P4 B9 K// JDBC driver name and database URL( ^( U m' ~, o* L
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
* z% `3 ^( z7 |% R6 P Qstatic final String DB_URL = "jdbc:mysql://localhost:3306/iac_enrollment_system";
. M) U) p1 F9 k j3 ^0 j// Database credentials
1 {4 V+ \2 {6 }$ T% S0 v1 e T5 k7 E# ostatic final String USER = "root";( F# O- C% N" P& E6 M" }' X9 Z
static final String PASS = "1234";
~: J# f0 B( r, d# Z0 opublic static void main(String[] args) {
; \* }! h9 z1 e' \0 o' Q Connection conn = null;4 X' K0 u) g+ _: Q: ? R
Statement stmt = null;% f6 z7 h; ?0 P
Scanner scn = new Scanner(System.in);/ H) {( ^& N/ R' T
String course_code = null, course_desc = null, course_chair = null;* ]( ^9 n+ A! W
try {
3 \$ L- m S: l+ z1 t9 d: o: Y // STEP 2: Register JDBC driver% P( F; v9 A# I% ?0 @
Class.forName("com.mysql.jdbc.Driver");
7 V* ~3 Y9 z3 `# o# ^- V2 M // STEP 3: Open a connection
9 s3 M# K1 P. u3 b' ^* A System.out.print("\nConnecting to database...");" T* x% {2 r+ e8 \! w0 U4 L
conn = DriverManager.getConnection(DB_URL, USER, PASS);
5 K( R6 R/ T' j1 W& u, Z3 b. P System.out.println(" SUCCESS!\n");
3 O* o1 G# t1 z, z, _ // STEP 4: Ask for user input
, n& I2 T, _+ v% ?3 ^: _* z5 d System.out.print("Enter course code: ");
- j8 X: k" R. t& {* D course_code = scn.nextLine();
5 [$ W; X9 P+ n s System.out.print("Enter course description: ");
- j- P( P. X" C( z. B- y: B course_desc = scn.nextLine();" F6 W. x; q# C5 B- F4 ~; l
System.out.print("Enter course chair: ");! [+ w! x9 g# B2 M% X
course_chair = scn.nextLine();
- I6 q, P: Z# c // STEP 5: Excute query
0 i- \$ z7 z, c+ w1 c' Z System.out.print("\nInserting records into table...");+ |- m, o( d! R3 E8 |
stmt = conn.createStatement();
7 Z# A% M; ^/ z* e' ~1 y String sql = "INSERT INTO course " +
4 N2 O! q/ E$ T( R, h- l$ _; q& W: ^ "VALUES (course_code, course_desc, course_chair)";+ E7 V6 `$ t2 D; c7 d
stmt.executeUpdate(sql);
( V; L6 M! A( r8 n7 ^+ l9 c2 g System.out.println(" SUCCESS!\n"); ^( K7 b: G& A2 \
} catch(SQLException se) {; D" J7 [0 e6 I6 \6 S
se.printStackTrace();
1 X2 I( e; X( l, ~/ Y$ E8 O# i } catch(Exception e) {
" j! r9 x/ X' [& L# p; `) a9 i e.printStackTrace();- t7 @2 o: b# O8 y
} finally {6 F7 {9 w+ }4 i, I9 T$ K& l$ D, h
try {) k; K8 \ K+ C9 N- p
if(stmt != null)! l. y+ }" ~' z2 x3 x
conn.close();
' Y3 D) W0 J- U- A* ~ } catch(SQLException se) {" W, P3 @5 i) U% r9 j( t- k, M
}+ }- }% t& L; @! {; o7 S
try {
0 S5 t3 P) g" w( M; N2 E if(conn != null); _1 ~# k2 f2 l: T
conn.close();
+ }# C8 u/ p2 q+ i! c } catch(SQLException se) {
* s" ]5 e* y' v T) j, J se.printStackTrace();* M- l& j5 ?+ t& |# u
}
9 ~& J( b1 P+ E" t( r8 n) a5 d }
7 c2 x: V/ |% j3 ~9 P; B Q- T System.out.println("Thank you for your patronage!");4 e$ S s8 m6 J* ]8 z2 Q# J! b
}
$ t" H0 K: K$ P& [0 L8 L& W}. ^3 J% B4 R$ W9 f% v
输出似乎成功返回:
! z! Y1 }$ A7 e! m8 ^
' K$ p; ~( V7 g3 s* c5 _6 U) k( F3 v: Z; i) f
但是,当我从MySQL中选择时,插入的记录为空:
1 w% s/ G7 A: i
0 Y% w" m0 K! m% W/ Q& x0 N5 R# \8 Z. L: T" L+ S% h0 W. ^
9 N9 T. k* k5 t! c为什么插入空白记录?/ Z# f1 `- S0 H, R! \
, O# x0 W. O& I: h2 {: k解决方案:/ c! g q8 ?1 o) D r4 x
/ Q/ ~3 U6 x, W7 E( G
8 ]+ L* `6 F9 p3 {1 a
8 T2 A0 ]" |) q# w# { 不,这是行不通的(不适用于真实数据):
* G4 X8 r5 O: c fString sql = "INSERT INTO course " +; r4 w- X, E# c: x/ l
"VALUES (course_code, course_desc, course_chair)";
1 H6 o* l2 r1 h/ { stmt.executeUpdate(sql);
& ^3 {# }5 j/ p0 _更改为:
# D6 s" o' p. d' U7 tString sql = "INSERT INTO course (course_code, course_desc, course_chair)" +% p2 u9 w5 h; U( w& T7 `
"VALUES (?, ?, ?)";# Y6 Z w9 y+ a; o5 X5 |
使用该sql创建一个PreparedStatment,并使用索引插入值:2 f" o9 l, A- E
PreparedStatement preparedStatement = conn.prepareStatement(sql);
5 P% u& |$ x8 J F/ ZpreparedStatement.setString(1, "Test");
0 T6 a; }7 e" u6 K0 m- QpreparedStatement.setString(2, "Test2");# U& l) B s3 F3 S
preparedStatement.setString(3, "Test3");" i0 t! W; W8 N& E* _) S
preparedStatement.executeUpdate(); |
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?立即注册
x
|