質問編集履歴
2
実行手順の追加
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
|
1
|
+
データベースにinsertできない。
|
body
CHANGED
@@ -1,152 +1,172 @@
|
|
1
|
-
以下のtestクラスを実行するとデータベースにデータの格納ができるのですが、
|
2
|
-
/**testクラスtestクラスtestクラスtestクラスtestクラスtestクラスtestクラス*/
|
3
|
-
package drinkMachine;
|
1
|
+
以下のtestクラスを実行するとデータベースにデータの格納ができるのですが、
|
4
2
|
|
5
|
-
import java.sql.Connection;
|
6
|
-
import java.sql.DriverManager;
|
7
|
-
|
3
|
+
servletクラスでgetParameterを用い、jspページに入力された値を受け取り、
|
8
|
-
import java.sql.SQLException;
|
9
4
|
|
10
|
-
|
5
|
+
Daoクラスに用意したsql文実行用のメソッド(testクラスと同じ内容の、
|
11
6
|
|
12
|
-
|
7
|
+
データをinsertするsql文)を用いてデータベースに値を格納しようとすると
|
13
|
-
// TODO 自動生成されたメソッド・スタブ
|
14
|
-
try{
|
15
|
-
Connection conn = null;
|
16
8
|
|
17
|
-
|
9
|
+
メソッドの戻り値が『ClassNotFoundException』になってしまいデータの格納ができません。
|
18
10
|
|
19
|
-
String url = "jdbc:mysql://localhost/example1";
|
20
|
-
|
11
|
+
自分では解決できないのでご協力お願いいたします。
|
21
|
-
String password = "*******";
|
22
12
|
|
13
|
+
<<実行手順>>
|
23
|
-
|
14
|
+
動的WEBプロジェクト<プロパティー<javaのビルド・パス<ライブラリー<外部jarの追加でjdbcドライバーmysql-connecter-java-5.1.40-bin.jarを追加しています。
|
24
15
|
|
25
|
-
String sql = "INSERT INTO drinkMachine(code,name,unitprice,count) VALUES(?,?,?,?)";
|
26
|
-
|
16
|
+
==========================testクラス=========================
|
27
|
-
pstmt.setString(1,"3");
|
28
|
-
pstmt.setString(2,"オレンジジュース");
|
29
|
-
pstmt.setString(3,"2");
|
30
|
-
pstmt.setString(4,"2");
|
31
|
-
int num = pstmt.executeUpdate();
|
32
|
-
System.out.println(num);
|
33
17
|
|
34
|
-
}catch(ClassNotFoundException e){
|
35
|
-
|
18
|
+
package drinkMachine;
|
36
|
-
System.out.println("a");
|
37
|
-
}catch(SQLException e){
|
38
|
-
e.printStackTrace();
|
39
|
-
System.out.println("b");
|
40
|
-
}
|
41
|
-
}
|
42
|
-
}
|
43
|
-
/**testクラスtestクラスtestクラスtestクラスtestクラスtestクラス*/
|
44
19
|
|
45
|
-
/**jspページjspページjspページjspページjspページjspページ*/
|
46
|
-
<%@ page language="java" contentType="text/html; charset=UTF-8"
|
47
|
-
pageEncoding="UTF-8"%>
|
48
|
-
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
49
|
-
<html>
|
50
|
-
<head>
|
51
|
-
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
|
52
|
-
<title>Insert title here</title>
|
53
|
-
|
20
|
+
import java.sql.*;
|
54
|
-
<body>
|
55
|
-
<form action="/drinkMachine/addController" method="post">
|
56
|
-
商品コード<input type="text" name="code"><br>
|
57
|
-
商品名<input type="text" name="name"><br>
|
58
|
-
金額<input type="text" name="price"><br>
|
59
|
-
数量<input type="text" name="count"><br>
|
60
|
-
<input type="submit" value="送信">
|
61
|
-
</form>
|
62
|
-
</body>
|
63
|
-
</html>
|
64
|
-
/**jspページjspページjspページjspページjspページjspページ*/
|
65
21
|
|
66
|
-
/**servletクラスservletクラスservletクラスservletクラス*/
|
67
|
-
|
22
|
+
public class test {
|
68
23
|
|
69
|
-
|
24
|
+
public static void main(String[] args) {
|
25
|
+
// TODO 自動生成されたメソッド・スタブ
|
26
|
+
try{
|
70
|
-
|
27
|
+
Connection conn = null;
|
71
28
|
|
72
|
-
import javax.servlet.ServletException;
|
73
|
-
import javax.servlet.http.HttpServlet;
|
74
|
-
|
29
|
+
Class.forName("com.mysql.jdbc.Driver");
|
75
|
-
import javax.servlet.http.HttpServletResponse;
|
76
30
|
|
77
|
-
/
|
31
|
+
String url = "jdbc:mysql://localhost/example1";
|
78
|
-
|
32
|
+
String host = "root";
|
79
|
-
*/
|
80
|
-
|
33
|
+
String password = "******";
|
81
|
-
private static final long serialVersionUID = 1L;
|
82
34
|
|
83
|
-
/**
|
84
|
-
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
|
85
|
-
*/
|
86
|
-
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
87
|
-
// TODO Auto-generated method stub
|
88
|
-
|
35
|
+
conn = DriverManager.getConnection(url,host,password);
|
89
|
-
}
|
90
36
|
|
91
|
-
/**
|
92
|
-
|
37
|
+
String sql = "INSERT INTO drinkMachine(code,name,unitprice,count) VALUES(?,?,?,?)";
|
93
|
-
*/
|
94
|
-
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
95
|
-
// TODO Auto-generated method stub
|
96
|
-
doGet(request, response);
|
97
|
-
|
38
|
+
PreparedStatement pstmt = conn.prepareStatement(sql);
|
98
|
-
|
39
|
+
pstmt.setString(1,"3");
|
99
|
-
|
40
|
+
pstmt.setString(2,"オレンジジュース");
|
100
|
-
|
41
|
+
pstmt.setString(3,"2");
|
101
|
-
|
42
|
+
pstmt.setString(4,"2");
|
102
|
-
|
43
|
+
|
103
|
-
T001_ITEMDao ti = new T001_ITEMDao();
|
104
|
-
int
|
44
|
+
int num = pstmt.executeUpdate();
|
105
|
-
|
45
|
+
System.out.println(num);
|
106
|
-
}
|
107
|
-
}
|
108
|
-
/**servletクラスservletクラスservletクラスservletクラス*/
|
109
46
|
|
110
|
-
|
47
|
+
}catch(ClassNotFoundException e){
|
111
|
-
|
48
|
+
e.printStackTrace();
|
49
|
+
System.out.println("a");
|
50
|
+
}catch(SQLException e){
|
51
|
+
e.printStackTrace();
|
52
|
+
System.out.println("b");
|
53
|
+
}
|
54
|
+
}
|
55
|
+
}
|
56
|
+
==========================testクラス=========================
|
112
57
|
|
113
|
-
import java.sql.Connection;
|
114
|
-
import java.sql.DriverManager;
|
115
|
-
import java.sql.PreparedStatement;
|
116
|
-
import java.sql.SQLException;
|
117
58
|
|
118
|
-
public class T001_ITEMDao {
|
119
59
|
|
120
60
|
|
61
|
+
|
62
|
+
==========================jspページ==========================
|
63
|
+
|
64
|
+
<%@ page language="java" contentType="text/html; charset=UTF-8"
|
65
|
+
pageEncoding="UTF-8"%>
|
66
|
+
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
67
|
+
<html>
|
68
|
+
<head>
|
69
|
+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
|
70
|
+
<title>Insert title here</title>
|
71
|
+
</head>
|
72
|
+
<body>
|
73
|
+
<form action="/drinkMachine/addController" method="post">
|
74
|
+
商品コード<input type="text" name="code"><br>
|
75
|
+
商品名<input type="text" name="name"><br>
|
76
|
+
金額<input type="text" name="price"><br>
|
77
|
+
数量<input type="text" name="count"><br>
|
78
|
+
<input type="submit" value="送信">
|
79
|
+
</form>
|
80
|
+
</body>
|
81
|
+
</html>
|
82
|
+
==========================jspページ==========================
|
83
|
+
|
84
|
+
|
85
|
+
=========================servletクラス========================
|
86
|
+
|
87
|
+
package drinkMachine;
|
88
|
+
|
89
|
+
import java.io.*;
|
90
|
+
import javax.servlet.ServletException;
|
91
|
+
import javax.servlet.http.HttpServlet;
|
92
|
+
import javax.servlet.http.HttpServletRequest;
|
93
|
+
import javax.servlet.http.HttpServletResponse;
|
94
|
+
|
95
|
+
/**
|
96
|
+
•Servlet implementation class addController
|
97
|
+
*/
|
98
|
+
public class addController extends HttpServlet {
|
99
|
+
private static final long serialVersionUID = 1L;
|
100
|
+
|
101
|
+
/**
|
102
|
+
•@see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
|
103
|
+
*/
|
104
|
+
/**
|
105
|
+
•@see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
|
106
|
+
*/
|
107
|
+
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
108
|
+
// TODO Auto-generated method stub
|
109
|
+
doGet(request, response);
|
110
|
+
|
111
|
+
PrintWriter out = response.getWriter();
|
112
|
+
|
113
|
+
request.setCharacterEncoding("UTF-8");
|
114
|
+
String code = request.getParameter("code");
|
115
|
+
String name =request.getParameter("name");
|
116
|
+
String price = request.getParameter("price");
|
117
|
+
String count = request.getParameter("count");
|
118
|
+
T001_ITEMDao ti = new T001_ITEMDao();
|
119
|
+
|
120
|
+
int str = ti.addItem(name);
|
121
|
+
|
122
|
+
out.print(str);
|
123
|
+
}
|
124
|
+
}
|
125
|
+
=========================servletクラス========================
|
126
|
+
|
127
|
+
==========================Daoクラス===========================
|
128
|
+
|
129
|
+
package drinkMachine;
|
130
|
+
|
131
|
+
import java.sql.Connection;
|
132
|
+
import java.sql.DriverManager;
|
133
|
+
import java.sql.PreparedStatement;
|
134
|
+
import java.sql.SQLException;
|
135
|
+
|
136
|
+
public class T001_ITEMDao {
|
137
|
+
|
121
138
|
public int addItem(String name){
|
122
|
-
try{
|
123
139
|
|
124
|
-
|
140
|
+
try{
|
125
141
|
|
126
|
-
|
142
|
+
Connection conn = null;
|
127
143
|
|
128
|
-
String url = "jdbc:mysql://localhost/example1";
|
129
|
-
String host = "root";
|
130
|
-
|
144
|
+
Class.forName("com.mysql.jdbc.Driver");
|
131
145
|
|
132
|
-
|
146
|
+
String url = "jdbc:mysql://localhost/example1";
|
147
|
+
String host = "root";
|
148
|
+
String password = "******";
|
133
149
|
|
134
|
-
String sql = "INSERT INTO drinkMachine(code,name,unitprice,count) VALUES(?,?,?,?)";
|
135
|
-
|
150
|
+
conn = DriverManager.getConnection(url,host,password);
|
136
|
-
pstmt.setString(1,"3");
|
137
|
-
pstmt.setString(2,"オレンジジュース");
|
138
|
-
pstmt.setString(3,"2");
|
139
|
-
pstmt.setString(4,"2");
|
140
|
-
int num = pstmt.executeUpdate();
|
141
151
|
|
152
|
+
String sql = "INSERT INTO drinkMachine(code,name,unitprice,count) VALUES(?,?,?,?)";
|
153
|
+
PreparedStatement pstmt = conn.prepareStatement(sql);
|
154
|
+
pstmt.setString(1,"3");
|
155
|
+
pstmt.setString(2,"オレンジジュース");
|
156
|
+
pstmt.setString(3,"2");
|
157
|
+
pstmt.setString(4,"2");
|
158
|
+
|
159
|
+
int num = pstmt.executeUpdate();
|
160
|
+
|
142
|
-
|
161
|
+
return num;
|
162
|
+
|
143
|
-
|
163
|
+
}catch(ClassNotFoundException e){
|
144
|
-
|
164
|
+
e.printStackTrace();
|
145
|
-
|
165
|
+
return 2;
|
146
|
-
|
166
|
+
}catch(SQLException e){
|
147
|
-
|
167
|
+
e.printStackTrace();
|
148
|
-
|
168
|
+
return 3;
|
149
|
-
|
169
|
+
}
|
150
|
-
|
170
|
+
}
|
151
|
-
}
|
171
|
+
}
|
152
|
-
|
172
|
+
========================Daoクラス===========================
|
1
パスワードが見えていたので隠した
title
CHANGED
File without changes
|
body
CHANGED
@@ -18,7 +18,7 @@
|
|
18
18
|
|
19
19
|
String url = "jdbc:mysql://localhost/example1";
|
20
20
|
String host = "root";
|
21
|
-
String password = "
|
21
|
+
String password = "*******";
|
22
22
|
|
23
23
|
conn = DriverManager.getConnection(url,host,password);
|
24
24
|
|
@@ -127,7 +127,7 @@
|
|
127
127
|
|
128
128
|
String url = "jdbc:mysql://localhost/example1";
|
129
129
|
String host = "root";
|
130
|
-
String password = "
|
130
|
+
String password = "******";
|
131
131
|
|
132
132
|
conn = DriverManager.getConnection(url,host,password);
|
133
133
|
|