Results 1 to 10 of 10
- 04-11-2012, 08:20 PM #1
Member
- Join Date
- May 2011
- Posts
- 42
- Rep Power
- 0
Help with JDBC Inserting Row&column
well im making GUI application which when i enter the data in textfield it will automatically gets entered in MS ACCESS database..fine...
i created rows and columns in MS ACCESS like
Java Code:SID SNAME BID BNAME ISSUE RETURN
i took class with students and enter data sid and sname and it apears like this
Java Code:SID SNAME BID BNAME ISSUE RETURN 1 a 2 d 3 f 4 d 5 s 4 d
FIne..but when i took class books and enter bid &bname it enters like
but i want likeJava Code:SID SNAME BID BNAME ISSUE RETURN 1 a 2 d 3 f 4 d 5 s 4 d 32 C book 433 JAVA
what exactly i need to to do in Result set..please help mee....Java Code:SID SNAME BID BNAME ISSUE RETURN 1 a 32 C Book 2 d 433 JAVA 3 f 4 d 5 s 4 d
PLEASE DO PROVIDE WHAT CODE I NEED TO INSERT AND EXPLAIN IT....
Here my Book class code..
NOTE:-Java Code:import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; import java.io.*; import java.awt.event.*; import java.sql.*; class book extends Frame implements ActionListener,ItemListener{ Button ad,bck; Label bkn,bkid,hd,conf,bklst; TextField bin,bii; Choice up; ResultSet rs; Connection con; PreparedStatement ps; Statement stmt; public book(){ Font f=new Font("Arial",Font.BOLD,15); Font n=new Font("Arial",Font.PLAIN,15); Font g=new Font("Arial",Font.BOLD+Font.ITALIC,30); hd=new Label("BOOKS LOG",Label.CENTER); bkid=new Label("Book ID:",Label.LEFT); bkn=new Label("Book Name:",Label.LEFT); bklst=new Label("Books List:",Label.LEFT); conf=new Label(); bii=new TextField(20); bin=new TextField(20); up=new Choice(); ad=new Button("Add"); bck=new Button("Back"); hd.setFont(g); bck.setFont(f); ad.setFont(n); bkn.setFont(f); bklst.setFont(f); conf.setFont(f); bkid.setFont(f); bii.setFont(n); bin.setFont(n); hd.setBounds(80,50,300,100); bkid.setBounds(80,200,70,40); bkn.setBounds(80,245,90,40); bklst.setBounds(80,290,90,40); bii.setBounds(180,205,150,26); bin.setBounds(180,250,150,26); up.setBounds(180,300,150,26); conf.setBounds(180,350,200,26); bck.setBounds(100,410,60,40); ad.setBounds(330,410,60,40); setLayout(null); add(up); add(bklst); add(ad); add(bck); add(bkn); add(bkid); add(hd); add(bin); add(bii); add(conf); ad.addActionListener(this); up.addItemListener(this); bck.addActionListener(this); try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con=DriverManager.getConnection("jdbc:odbc:mydb"); stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); rs=stmt.executeQuery("select * from mydb"); rs.next(); } catch(Exception e){} } public void itemStateChanged(ItemEvent e){ repaint(); } public void actionPerformed(ActionEvent ae){ String record; if(ae.getSource()==ad){ try { int bkidd=Integer.parseInt(bii.getText()); String bknm=(bin.getText()); stmt.executeUpdate("insert into mydb(BookID,BookName) values("+bkidd+",'"+bknm+"')"); conf.setText("BOOK ADDED"); dbClose(); dbOpen(); } catch (java.sql.SQLException se) { se.printStackTrace(); } } if(ae.getSource()==bck){ GetFrame2 f2=new GetFrame2(); f2.setVisible(true); f2.setSize(500,500); this.dispose(); } } public void dbOpen() { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con=DriverManager.getConnection("jdbc:odbc:mydb"); stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); rs=stmt.executeQuery("select * from mydb"); rs.next(); }catch(Exception e){} } public void dbClose() { try{ stmt.close(); rs.close(); con.close(); }catch(Exception e){} } public static void main(String args[]){ book bks=new book(); } }
Above code is Book.Java filewhich has bid and bname. I have another file that is Students.java which have sid sname information and Database coding is same as Book.java fileLast edited by danpotter; 04-11-2012 at 09:50 PM.
- 04-11-2012, 09:10 PM #2
Re: Help with JDBC Inserting Row&column
To achieve what you are trying to do, you need to do an UPDATE after you insert the initial columns with the SID and SNAME, maybe something like(assuming the BID column holds integers):
UPDATE MYTABLE SET BID = 32, BNAME = 'C Book' WHERE SID= 1 AND SNAME='a'
UPDATE MYTABLE SET BID = 433, BNAME = 'JAVA' WHERE SID= 1 AND SNAME='a'
If you post some of the code that you have, I may be able to offer more direct help, but the general idea is that if you already have the rows with SID and SNAME data, you need to do an UPDATE for each record that you want to add BID and BNAME data for, not an INSERT, as this creates new records(rows), which isn't what you want.Last edited by sehudson; 04-11-2012 at 09:16 PM.
- 04-11-2012, 09:28 PM #3
Re: Help with JDBC Inserting Row&column
Moved from New to Java.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 04-11-2012, 09:34 PM #4
Member
- Join Date
- May 2011
- Posts
- 42
- Rep Power
- 0
Re: Help with JDBC Inserting Row&column
well i need to enter values after running the program,in GUI application..so is it should be like this??
if required..i will surely post the code...Java Code:String ups; ups = "UPDATE MYTABLE SET BID = ' ', BNAME = ' ' WHERE SID= 1 AND SNAME='a' stmt.executeUpdate(ups);
- 04-11-2012, 10:17 PM #5
Re: Help with JDBC Inserting Row&column
Few things:
-You can combine lines 1 and 2 into a single line, by declaring and initializing your string with 1 line:
-Class names should be capitalized, so "Book" instead of "book".Java Code:String ups = "UPDATE MYTABLE SET BID = ' ', BNAME = ' ' WHERE SID= 1 AND SNAME='a'";
-Just an idea. Instead of creating records for a student, and then updating them later, what if you created a method in your Student class called checkoutBook, and you pass that method a Book object as an argument, and inside the method, a record is created for that student in the database with his sid, sname(from the Student), and bname, bid(from the Book).
So it would look something like this:
Java Code:public void checkoutBook(Book bookToBeCheckedOut){ //Call to database to update }Last edited by sehudson; 04-11-2012 at 10:40 PM.
- 04-12-2012, 06:03 AM #6
Member
- Join Date
- May 2011
- Posts
- 42
- Rep Power
- 0
Re: Help with JDBC Inserting Row&column
yah no problem with above two things..i know.....
well your idea is really good,but as im working in a collage proj..,i have to work my sir says and he is saying to do only in this way[what im doing now...]so pls guide me in that way only.....
- 04-12-2012, 09:47 AM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: Help with JDBC Inserting Row&column
Not too sure about the database model, but I'm presuming that was handed to you to work with.
How are you supposed to identify which line the book is to be added to?
In the example you give in your first post you have to rows with an sid of '4' and an sname of 'd', so there is no possible update that can be done which will single out just one of these lines for update.Please do not ask for code as refusal often offends.
- 04-12-2012, 04:15 PM #8
Member
- Join Date
- May 2011
- Posts
- 42
- Rep Power
- 0
- 04-12-2012, 04:27 PM #9
Re: Help with JDBC Inserting Row&column
Tolls is saying that based on what you provided, you initially have multiple records containing the same SID and SNAME (look at rows 5 and 7 of your 2nd code snippet in your 1st post).
If you want to add the book data to a record, how would you know whether row 5 is to be updated or if row 7 is to be updated? When you do an SQL UPDATE, you would distinguish between records by using a WHERE clause and specify some particular row/rows you want to update, but since you have 2 rows with identical SNAME and SID values, BOTH of those rows will be updated.
So basically, it seems like there is something flawed with the way the database table is setup, or, your explanation isn't correct.
- 04-13-2012, 09:30 AM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Similar Threads
-
insert row and column and delete row and column
By daredavil82 in forum New To JavaReplies: 13Last Post: 09-22-2011, 06:10 PM -
jdbc: invalild column index from statement.execute():
By ernestcronin in forum JDBCReplies: 14Last Post: 08-02-2011, 04:40 PM -
How to write column by column to text file
By trkece in forum JDBCReplies: 9Last Post: 02-15-2011, 01:13 AM -
selecting column names dynamically from table column header
By neha_sinha in forum AWT / SwingReplies: 1Last Post: 07-06-2010, 04:50 PM -
Inserting Multiple Images into oracle database using JDBC
By raihan26 in forum JDBCReplies: 2Last Post: 04-01-2010, 04:35 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks