ورود

View Full Version : سوال: برنامه سرچ در دیتابیس جاوا JDBC



masoud120
یک شنبه 27 اردیبهشت 1394, 11:58 صبح
با عرض سلام و خسته نباشید
دوستان من یه برنامه سرچ تو دیتابیس میخاستم بنویسم ولی خب به نظر نحوه سرچ sql من اشتباس دوستان عزیز میتونید راهنمایی کنید چجوری باید کد سرچ و بنویسم ؟!




import java.sql.*;
import java.util.*;


public class JdbcSearchTest {
static Scanner console = new Scanner(System.in);
public static void main(String[] args) {
String strS;
strS=console.nextLine();
System.out.print(strS);
try(
// Step 1: Allocate a database "Connection" object
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/ebookshop", "root", "root"); // MySQL
// Step 2: Allocate a "Statement" object in the Connection
Statement stmt = conn.createStatement();


)
{
String strSearch="select title,price,qty from books where title like %"+strS+"%";
System.out.println("The SQL query is: " + strSearch); // Echo For debugging
System.out.println();
ResultSet rset=stmt.executeQuery(strSearch);
System.out.println("The records selected are:");
int rowCount = 0;
while(rset.next()) { // Move the cursor to the next row
String title = rset.getString("title");
double price = rset.getDouble("price");
int qty = rset.getInt("qty");
System.out.println(title + ", " + price + ", " + qty);
++rowCount;
}
System.out.println("Total number of records = " + rowCount);
}
catch (SQLException ex){
ex.printStackTrace();
}


}
}

ahmad.mo74
یک شنبه 27 اردیبهشت 1394, 21:55 عصر
سلام. یه اشتباه ریز انجام دادید.
title از جنس String ه و باید توی `` باشه.

یعنی query رو به این تغییر بدید :

String strSearch = "select title,price,qty from books where title like '%" + strS + "%'";

من کد رو براتون باز نویسی کردم :


public class JdbcSearchTest {


public static void main(String[] args) {
System.out.print("Enter Book Title : ");
String in;
try (Scanner scanner = new Scanner(System.in)) {
in = scanner.nextLine();
}
System.out.println();
try (Connection connection = getConnection("com.mysql.jdbc.Driver", "jdbc:mysql://localhost:3306/ebookshop", "root", "root")) {
if (connection != null) {
String searchQuery = "select title,price,qty from books where title like ?";
try (PreparedStatement statement = connection.prepareStatement(searchQuery)) {
statement.setString(1, "%" + in + "%");
System.out.println("The SQL query is : " + searchQuery);
System.out.println("The records selected are:");
ResultSet resultSet = statement.executeQuery();
int rowCount = 0;
while (resultSet.next()) {
String title = resultSet.getString("title");
double price = resultSet.getDouble("price");
int qty = resultSet.getInt("qty");
System.out.println(title + ", " + price + ", " + qty);
rowCount++;
}
System.out.println("Total number of records = " + rowCount);
}
}
} catch (SQLException e) {
e.printStackTrace();
}
}


static Connection getConnection(String driverClassName, String jdbcUrl, String username, String password) {
try {
Class.forName(driverClassName);
return DriverManager.getConnection(jdbcUrl, username, password);
} catch (SQLException | ClassNotFoundException e) {
e.printStackTrace();
}
return null;
}


}

masoud120
دوشنبه 28 اردیبهشت 1394, 21:03 عصر
ممنونم ازت مرسی دوست عزیز
مشکل حل شد :)