|
|
|
สอบถามการคอนเนค mysql ตอนส่งค่าไปที่ Doget หน่อยครับ JAVA |
|
|
|
|
|
|
|
คือเรื่องมีอยุ่ว่า ผมจะส่งค่า 123 จาก index.jsp ไปที่ servlet.java แล้วให้ทำการทดสอบว่าคิวรี่ข้อมูลออกได้ไหม ผลปรากฏว่าไม่สามารถ connect ได้ แต่ผมทดลองสร้างคลาสtest.java มาลองใช้ โค้ดเดียวกันในการต่อ ต่อได้ครับผม
คลาสส Dbconnection.java
Code (Java)
package com;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Dbconnection {
static Connection conn = null;
public static Connection ConnectDb() {
String classforname = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8";
String user = "root";
String password = "1234";
try
{
Class.forName(classforname);
conn = DriverManager.getConnection(url, user, password);
return conn;
}
catch (ClassNotFoundException | SQLException e)
{
System.out.println("Cannot Connection Database");
return null;
}
}
}
หน้า Index.jsp
Code
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="servlet" method="post">
Select Book Name : <input type="text" name="name">
<input type="submit" value="Submit"></form>
<h3> The Book Is <%= request.getParameter("name") %></h3>
</body>
</html>
หน้า servlet.java
Code (Java)
package com;
import com.Dbconnection;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/servlet")
public class Servlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public Servlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String n = request.getParameter("name");
System.out.println(n);
String sql = "select * from books WHERE isbn = "+n;
System.out.println(sql);
try{
//Connection conn = null;
//Connection conn = Dbconnection.ConnectDb();
//Statement st = null;
//ResultSet rs = null;
Statement st = Dbconnection.ConnectDb().createStatement();
ResultSet rs = st.executeQuery(sql);
while((rs!=null) && (rs.next())) {
String isbn = (rs.getString("ISBN")+" ");
String title =(rs.getString("TITLE")+" ");
String author =(rs.getString("AUTHOR")+" ");
String price =(rs.getInt("PRICE")+" ");
System.out.println(isbn);
System.out.println(title);
System.out.println(author);
System.out.println(price);
}
}
catch(Exception e){
System.out.println("Cannot Connect Database");
}
request.getRequestDispatcher("Index.jsp").forward(request, response);
}
}
ไฟล์ test.java
Code (Java)
package com;
import java.sql.ResultSet;
import java.sql.Statement;
import com.Dbconnection;
public class test {
public static void main(String[] args) {
//Connection conn = null;
//conn = Dbconnection.ConnectDb();
//Statement st = null;
//ResultSet rs = null;
String sql = "select * from books WHERE isbn = 123";
try{
Statement st = Dbconnection.ConnectDb().createStatement();
ResultSet rs = st.executeQuery(sql);
while((rs!=null) && (rs.next())) {
String isbn = (rs.getString("ISBN")+" ");
String title =(rs.getString("TITLE")+" ");
String author =(rs.getString("AUTHOR")+" ");
String price =(rs.getInt("PRICE")+" ");
System.out.print(isbn);
System.out.print(title);
System.out.print(author);
System.out.print(price);
}
}
catch(Exception e){
}
}
}
Tag : Java, JAVA, JSP
|
|
|
|
|
|
Date :
2018-02-28 10:59:45 |
By :
sontud34310 |
View :
1873 |
Reply :
3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ทั้ง doPost และ doget ไม่ได้ทั้งสองเลยครับ พอดีมือใหม่แนะนำทีนะครับ
|
|
|
|
|
Date :
2018-02-28 11:02:47 |
By :
sontud34310 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ได้ละครับผมใส่ lib ผิดที่กำจริงๆ
|
|
|
|
|
Date :
2018-02-28 13:16:10 |
By :
sontud34310 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Date :
2018-03-01 21:45:05 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 03
|