Tek Class ‘da Java Veritabanı İşlemleri

public class baglan {
    public static Connection con = null;
    private static String url = "jdbc:mysql://localhost:3306/alisveris";
    private static String driver = "com.mysql.jdbc.Driver";
    private static String dbname = "alisveris";
    private static String username = "root";
    private static String password = "root";
    static ResultSet rs = null;
    public static void openConnection(){
        try {
            Class.forName(driver).newInstance();
            con = (Connection) DriverManager.getConnection(url,username,password);
        } catch (Exception ex) {
            Logger.getLogger(baglan.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public static boolean veriSil(int id){
        try {
            PreparedStatement ps = (PreparedStatement) baglan.con.prepareStatement("delete from table where id=?");
            ps.setInt(1, id);
            ps.executeUpdate();
          return true;
        
        } catch (SQLException ex) {
            Logger.getLogger(baglan.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }
    public static boolean veriGuncelle(int id, String isim, int fiyat){
        try {
            // UPDATE table_name SET column1=value1,column2=value2 WHERE some_column=some_value;
            PreparedStatement ps = (PreparedStatement) baglan.con.prepareStatement("update table set ad=?,fiyat=? where id=?");
            ps.setString(1, isim);
            ps.setInt(1, fiyat);
            ps.setInt(1, id);
            ps.executeUpdate();
          return true;
        
        } catch (SQLException ex) {
            Logger.getLogger(baglan.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }
    public static ResultSet veriAl(int id){
        ResultSet rs = null;
        try {
            PreparedStatement ps = (PreparedStatement) baglan.con.prepareStatement("select * from table where id=?");
            ps.setInt(1, id);
            rs = ps.executeQuery(); 
            if(rs.next()){
                return rs;
            }
        } catch (SQLException ex) {
            Logger.getLogger(baglan.class.getName()).log(Level.SEVERE, null, ex);
        }
        return rs;
    }
    public static DefaultTableModel tableAktar(){
        DefaultTableModel tm = new DefaultTableModel();
        try {
            PreparedStatement ps = (PreparedStatement) baglan.con.prepareStatement("select * from table");
            rs = ps.executeQuery(); 
            int colCount = rs.getMetaData().getColumnCount(); // Toplam sütun sayısını alıyor
            for(int i = 1;i<colCount+1;i++){
                tm.addColumn(rs.getMetaData().getColumnName(i)); //Tabloya sütun ekliyor
            }
            while(rs.next()){
                Object[] row = new Object[colCount];
                    for(int i = 1;i<colCount+1;i++){
                        row[i-1]=(Object)rs.getObject(i);
                    }
                tm.addRow(row);
            }
        }catch(Exception ex){}
        return tm;
    }
}

buyursunlar :)



Leave a Reply

Formatting: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Other Entries

Kategoriler