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();
}
}
}
دوستان من یه برنامه سرچ تو دیتابیس میخاستم بنویسم ولی خب به نظر نحوه سرچ 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();
}
}
}