cnmeysam
پنج شنبه 14 بهمن 1395, 11:32 صبح
سلام ممنون از وقتی که میذارید
شاید درست نباشه اینجوری سوال رو بپرسم نمیدونم شاید باید 2تا تاپیک میزدم ولی اگه میشه راهنماییم کنید. سیستم عاملم اوبونتو هستش از netbeans و mysql استفاده میکنم
من تازه شروع به برنامه نویسی با جاوا کردم و تو قسمت کار با دیتابیس با مشکل مواجه شدم!!
اول برای اینکه مجبور نشم توی تمام فرم هام کانکشن استرینگ رو بنویسم یه کلاس برای کانکشن استرینگ ساختم و مشخصات کانکشن رو از داخل 3تا فایل متنی که توشون url , username,password رو ذخیره کردم فراخوانی کردم و ازشون استفاده کردم توی کلاس این کدهایی هست که برای کلاس کانکشن نوشتم اسم کلاسمم هم database هستش:
/* * To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package test;
//کتابخانه ارتباط با دیتابیس
import java.sql.*;
import java.util.logging.Logger;
import java.util.logging.Level;
import javax.swing.JOptionPane;
//کتابخانه ارتباط با دیتابیس
//کتابخانه mysql JDBC Driver باید اضافه شود به کتابخانه ها
//کتابخانه کار بار فایل متنی
import java.io.*;
import java.awt.Color;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Scanner;
import static test.connectiontest.con;
import static test.connectiontest.password;
import static test.connectiontest.url;
import static test.connectiontest.username;
//کتابخانه کار بار فایل متنی
/**
*
* @author o_0
*/
public class database {
//اتصال به دیتابیس (کانکشن استرینگ)
public static Connection con = null;
public static Statement stmt = null;
public static ResultSet resSet = null;
public static String username =null;
public static String password = null;
public static String url = null;
public static ResultSet getResultSet()
{
return ( resSet );
}
public static Connection getConnection()
{
return con;
}
//اتصال به دیتابیس
public static boolean connect_to_db()
{
try
{
FileReader reader = new FileReader("url.dll");
BufferedReader bufferedReader = new BufferedReader(reader);
String line;
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
url =line;
}
reader.close();
FileReader readeruser = new FileReader("user.dll");
BufferedReader bufferedReaderuser = new BufferedReader(readeruser);
String lineuser;
while ((lineuser = bufferedReaderuser.readLine()) != null) {
System.out.println(lineuser);
username =lineuser;
}
readeruser.close();
FileReader readerpass = new FileReader("pass.dll");
BufferedReader bufferedReaderpass = new BufferedReader(readerpass);
String linepass;
while ((linepass = bufferedReaderpass.readLine()) != null) {
System.out.println(linepass);
password =linepass;
}
readerpass.close();
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection(url,username,password) ;
return true;
}
catch( Exception e )
{
e.printStackTrace();
JOptionPane.showMessageDialog(null, "can not connect to db! "
+ e.getMessage());
return false;
}
}
//اتصال به دیتابیس
//کوئری یا سلکت به دیتابیس
public static boolean query_on_db( String _query )
{
try
{
stmt = con.createStatement();
resSet = stmt.executeQuery( _query );
if( resSet.next() )
{
return true;
}
else
{
return false;
}
}
catch( Exception e )
{
JOptionPane.showMessageDialog(null, "can not run query! "
+ e.getMessage());
return false;
}
}
//کوئری یا سلکت به دیتابیس
//آبدیت اینسرت و حذف از دیتابیس
public static boolean updateQuery(String update_query)
{
try
{
stmt =con.createStatement();
stmt.executeUpdate(update_query);
return true;
}
catch (Exception e)
{
JOptionPane.showMessageDialog(null,"can not run update query! " + e.getMessage());
return false;
}
}
//آبدیت اینسرت و حذف از دیتابیس
// دیسکانکت شدن از دیتابیس
public static boolean disconnect_from_db()
{
try
{
if( !con.isClosed() && !stmt.isClosed() && !resSet.isClosed() )
{
con.close();
stmt.close();
resSet.close();
return true;
}
else
{
return true;
}
}
catch( Exception e )
{
JOptionPane.showMessageDialog(null, "can not close connection!"
+ e.getMessage() );
return false;
}
}
}
// دیسکانکت شدن از دیتابیس
ولی 2تا مشکل دارم:
اول اینکه وقتی select میزنم برای لود شدن داده هام اولین داده ای که داخل دیتابیس ذخیره کردم لود نمیشه یعنی اکه مثلا من 4تا یوزر پس ذخیره کردم شماره 1 لود نمیشه و شماره 2و3و4 فقط توی table لود و نمایش داده میشه چیکار کنم که همه داده هام نمایش داده بشه؟
این کدی هست که برای لود دیتا توی table نوشتم:
if (database.connect_to_db())
{
if(database.query_on_db("SELECT * FROM users"))
{
tblusers.setModel(DbUtils.resultSetToTableModel(da tabase.getResultSet()));
}
database.disconnect_from_db();
}
و دوم کد سرچ هم درست کار نمیکنه وقتی جستجو میکنم فقط آخرین فیلدی که ذخیره کردم لود میشه یعنی اگه مثلا فیلد دومم توی دیتابیس a باشه جستجو کنم چیزی نمایش نمیده ولی اگه فیلد آخرم توی دیتابیس b باشه سرچ کنم محتویاتش توی تیبل نمایش داده میشه
این هم کد جستجو هستش:
String n = txtsearch.getText();
if (database.connect_to_db())
{
if (database.query_on_db("SELECT * FROM users where Tbl_User like '%"+n+"%'"))
{
try
{
tblusers.setModel(DbUtils.resultSetToTableModel(da tabase.getResultSet()));
database.disconnect_from_db();
}
catch (Exception e)
{
JOptionPane.showMessageDialog(null, e.getMessage());
}
}
شاید درست نباشه اینجوری سوال رو بپرسم نمیدونم شاید باید 2تا تاپیک میزدم ولی اگه میشه راهنماییم کنید. سیستم عاملم اوبونتو هستش از netbeans و mysql استفاده میکنم
من تازه شروع به برنامه نویسی با جاوا کردم و تو قسمت کار با دیتابیس با مشکل مواجه شدم!!
اول برای اینکه مجبور نشم توی تمام فرم هام کانکشن استرینگ رو بنویسم یه کلاس برای کانکشن استرینگ ساختم و مشخصات کانکشن رو از داخل 3تا فایل متنی که توشون url , username,password رو ذخیره کردم فراخوانی کردم و ازشون استفاده کردم توی کلاس این کدهایی هست که برای کلاس کانکشن نوشتم اسم کلاسمم هم database هستش:
/* * To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package test;
//کتابخانه ارتباط با دیتابیس
import java.sql.*;
import java.util.logging.Logger;
import java.util.logging.Level;
import javax.swing.JOptionPane;
//کتابخانه ارتباط با دیتابیس
//کتابخانه mysql JDBC Driver باید اضافه شود به کتابخانه ها
//کتابخانه کار بار فایل متنی
import java.io.*;
import java.awt.Color;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Scanner;
import static test.connectiontest.con;
import static test.connectiontest.password;
import static test.connectiontest.url;
import static test.connectiontest.username;
//کتابخانه کار بار فایل متنی
/**
*
* @author o_0
*/
public class database {
//اتصال به دیتابیس (کانکشن استرینگ)
public static Connection con = null;
public static Statement stmt = null;
public static ResultSet resSet = null;
public static String username =null;
public static String password = null;
public static String url = null;
public static ResultSet getResultSet()
{
return ( resSet );
}
public static Connection getConnection()
{
return con;
}
//اتصال به دیتابیس
public static boolean connect_to_db()
{
try
{
FileReader reader = new FileReader("url.dll");
BufferedReader bufferedReader = new BufferedReader(reader);
String line;
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
url =line;
}
reader.close();
FileReader readeruser = new FileReader("user.dll");
BufferedReader bufferedReaderuser = new BufferedReader(readeruser);
String lineuser;
while ((lineuser = bufferedReaderuser.readLine()) != null) {
System.out.println(lineuser);
username =lineuser;
}
readeruser.close();
FileReader readerpass = new FileReader("pass.dll");
BufferedReader bufferedReaderpass = new BufferedReader(readerpass);
String linepass;
while ((linepass = bufferedReaderpass.readLine()) != null) {
System.out.println(linepass);
password =linepass;
}
readerpass.close();
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection(url,username,password) ;
return true;
}
catch( Exception e )
{
e.printStackTrace();
JOptionPane.showMessageDialog(null, "can not connect to db! "
+ e.getMessage());
return false;
}
}
//اتصال به دیتابیس
//کوئری یا سلکت به دیتابیس
public static boolean query_on_db( String _query )
{
try
{
stmt = con.createStatement();
resSet = stmt.executeQuery( _query );
if( resSet.next() )
{
return true;
}
else
{
return false;
}
}
catch( Exception e )
{
JOptionPane.showMessageDialog(null, "can not run query! "
+ e.getMessage());
return false;
}
}
//کوئری یا سلکت به دیتابیس
//آبدیت اینسرت و حذف از دیتابیس
public static boolean updateQuery(String update_query)
{
try
{
stmt =con.createStatement();
stmt.executeUpdate(update_query);
return true;
}
catch (Exception e)
{
JOptionPane.showMessageDialog(null,"can not run update query! " + e.getMessage());
return false;
}
}
//آبدیت اینسرت و حذف از دیتابیس
// دیسکانکت شدن از دیتابیس
public static boolean disconnect_from_db()
{
try
{
if( !con.isClosed() && !stmt.isClosed() && !resSet.isClosed() )
{
con.close();
stmt.close();
resSet.close();
return true;
}
else
{
return true;
}
}
catch( Exception e )
{
JOptionPane.showMessageDialog(null, "can not close connection!"
+ e.getMessage() );
return false;
}
}
}
// دیسکانکت شدن از دیتابیس
ولی 2تا مشکل دارم:
اول اینکه وقتی select میزنم برای لود شدن داده هام اولین داده ای که داخل دیتابیس ذخیره کردم لود نمیشه یعنی اکه مثلا من 4تا یوزر پس ذخیره کردم شماره 1 لود نمیشه و شماره 2و3و4 فقط توی table لود و نمایش داده میشه چیکار کنم که همه داده هام نمایش داده بشه؟
این کدی هست که برای لود دیتا توی table نوشتم:
if (database.connect_to_db())
{
if(database.query_on_db("SELECT * FROM users"))
{
tblusers.setModel(DbUtils.resultSetToTableModel(da tabase.getResultSet()));
}
database.disconnect_from_db();
}
و دوم کد سرچ هم درست کار نمیکنه وقتی جستجو میکنم فقط آخرین فیلدی که ذخیره کردم لود میشه یعنی اگه مثلا فیلد دومم توی دیتابیس a باشه جستجو کنم چیزی نمایش نمیده ولی اگه فیلد آخرم توی دیتابیس b باشه سرچ کنم محتویاتش توی تیبل نمایش داده میشه
این هم کد جستجو هستش:
String n = txtsearch.getText();
if (database.connect_to_db())
{
if (database.query_on_db("SELECT * FROM users where Tbl_User like '%"+n+"%'"))
{
try
{
tblusers.setModel(DbUtils.resultSetToTableModel(da tabase.getResultSet()));
database.disconnect_from_db();
}
catch (Exception e)
{
JOptionPane.showMessageDialog(null, e.getMessage());
}
}