- Accessing MySQL on NetBeans using JDBC, Part 1: Create a connection
- Accessing MySQL on NetBeans using JDBC, Part 2: Perform SQL Operations
Accessing MySQL on NetBeans using JDBC, Part 2: Perform SQL Operations
From Part I, I have only established a connection with local MySQL. Next I’ll show how to retrieve and modify data on remote MySQL.
MySQL Connection using in this part
Suppose that I have MySQL running remotely on IP:192.168.1.101 with default port (3306) and I want to connect to Northwind database with username is ‘root’ and password is ‘123456’. The connection string will be
String connectionUrl = "jdbc:mysql://192.168.1.101:3306/Northwind?" + "user=root&password=123456"; |
Retrieve data from a database
To get some data, I need to execute query on the MySQL and get the result back to me. First, I create stmt (Statement object) and execute query in SQL language. Then I store the result on ResultSet object and iterative show the result on the output window.
Statement stmt = null; ResultSet rs = null; //SQL query command String SQL = "SELECT * FROM Products"; stmt = con.createStatement(); rs = stmt.executeQuery(SQL); while (rs.next()) { System.out.println(rs.getString("ProductName") + " : " + rs.getString("UnitPrice")); } |
Code Explanation:
– Statement objects allow you to execute basic SQL queries and retrieve the results through the ResultSet class.
– In while-loop, iterative in the ResultSet object to show result in console (ProductName and UnitPrice columns in Products table) on output window.
The example result will be similar to below.
Note: I have imported only 4 records from Products table in Northwind database.
Update data on database
To insert, update and delete records on SQL Server, you can use the code from retrieve data from database and simply change SQL command and also modify some code a little bit. On update, I must use executeUpdate(“SQL”) method on statement object instead executeQuery(“SQL”) and the return value will be rows affected instead of a record set.
Example
INSERT command
// SQL insert command String strSQL = "INSERT INTO Products (ProductName,QuantityPerUnit,UnitPrice,UnitsInStock,UnitsOnOrder," + "ReOrderLevel,Discontinued) VALUES ('MyProduct','10 Kg.',1234.0000,100,50,30,0)"; int rowsEffected = stmt.executeUpdate(strSQL); System.out.println(rowsEffected + " rows effected"); |
UPDATE command
// SQL update command String strSQL = "UPDATE Products SET UnitPrice = 900, UnitsInStock = 55, UnitsOnOrder = 5 WHERE ProductName = 'MyProduct'"; int rowsEffected = stmt.executeUpdate(strSQL); System.out.println(rowsEffected + " rows effected"); |
DELETE command
// SQL delete command String strSQL = "DELETE FROM Products WHERE ProductName = 'MyProduct'"); int rowsEffected = stmt.executeUpdate(strSQL); System.out.println(rowsEffected + " rows effected"); |
Summary
You can download source code example testMySQL.java (Right-click on the link and select Save target As…).
But you have to change connection string to match your environment. The example code will connect to Northwind database and try to retrieve records, insert a new record, update the record and delete the record from Products table. The result is below.
Thanks a lot! That is exactly what I needed to get jump-started on Java & MySQL.
Very good.
You have no idea how hard it is to find a simple explanation like this.
can i ask u question
why i cannot build because of the con.
The result of the compiler is above
symbol : variable con
location: class test.test
stmt = con.createStatement();
1 error
BUILD FAILED (total time: 0 seconds)
who can help me !?
I think that you haven’t completed part1: create a connection yet. You need to create a connection to MySQL before performing any query. Try to read part1 first.
Thanks a lot. i had solved my problem
Thank you for this tutorial. Very usefull indeed. I have a problem with this line:
stmt = con.createStatement();
when I write it in my Netbeans IDE, it gets red underlined, the error message says: “Cannot find symbol”
Any idea why?
NB: on the following line:
Connection con = DriverManager.getConnection(connectionUrl);
The line is underlined grey saying that the con variable is not being used. That might be the source of the problem…
I import the followings:
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.Connection;
and use IDE 6.1 and MySQL 5.1, connector 5.1.6
Hi, Arnaud
Normally, when you see this error message, it’ll tell which symbol. For instance,
cannot find symbol
symbol : variable aaa
location: class Main
This means that you haven’t declare a variable ‘aaa’ before using it.
According to your problem, have you already declare all of variables? Try check stmt and connectionUrl. In my example above, I have declare stmt = null and connectionUrl = “jdbc:mysql…..”.
@Arnaud: You have to put the code of this part inside the scope of the “try” command from Part I.
If you place the code outside, then the compiler doesn’t see the declaration for the Connection con.
Thanks Dory, that was the answer to my problem. As I said very new to java programming.
Sorry that was very basic.
Thank you both for your answers.
By the way anybody knows where I could find a tutorial to then bind this query (INNER JOIN through more than 1 table) to a JTable?
The NetBeans tutorials only shows how to bind a simple table to a JTable, which is not very usefull as most of my tables contains ID’s we are of no meaning to the interface use.
Thank you
It’s great!!! I was trying to find out a place to start JAVA MYSQL connection and found no where. All the writers assume that only professionals have the right to read their articles.
Thanks a lot for the simple but great and effective article.
thnks a lot
it worked for me
Nice Job.
Obrigado!!!
How to insert, update and delete if we use has map method?
Can you explain about it???
Hi, inD_05
What do you means “map method”?
Hi, I have followed your examples (parts I & II) and it compiles fine, BUT when invoking:
DriverManager.getConnection(url, user, pass);
it returns an error that i cant resolve by now:
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
Last packet sent to the server was 0 ms ago.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
…
can you enlighten me here? THANKS!
solved it 🙂 some how the “:port/” part of the conn string was causing troubles.
Hi! Thanks for the great tutorial. All I need to know now is how I show the resultset on grid in Java using NetBeans
thanks so very mach:
muy buen tutorial.
great tutorial.
Cheers, Dude,
you’ve saved me loads of time!
All the best,
to
thank’s a lot
the world needs people like you
It helps in a simple way
Efectivamente,
muchas gracias por la demostración.
Thanks a lot. the tutorials helped me to start my journey with Java. Expecting a lot from you genius
I have a table named “Ciudades” with the following structure:
cod_ciudad = int
nom_ciudad = varchar
cod_provincia = varchar
I want to retrieve data from the table with the following code but instead I get nothing.
(the connection has already been created and opened)
sSentencia = m_cConexion.createStatement();
rsResultado = sSentencia.executeQuery(“SELECT * FROM Ciudades ” +
“WHERE cod_ciudad >= ” + Integer.toString(iCodigoDesde) +
” AND cod_ciudad <= " + Integer.toString(iCodigoHasta));
What am I doing wrong?
Thanks in advance.
Hi, Veronica
Is there any error message? First, you should try to test if the SQL query’s syntax is correct by print out the query to console window and copy it to run on the SQL Server.
I followed ur instruction step by step.. it successfully build but fail to run..
SQL Exception: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure.
The last packet sent successfully to the server was 0ms ago. The driver has not received any packets from the server.
Why is this so? What should I check in this case?
Java Beginner
Hi, Sitti
Check if the MySQL is accepting the connection from your application. Or if there is any firewall blocking.
Hi linglom,
Nothing change when I disable the firewall… How do i check for if MySQL is accepting the connection?? Is my connection string correct?? =)
try
{
Class.forName(“com.mysql.jdbc.Driver”);
String connectionUrl = “jdbc:mysql://localhost/Northwinds?;”;
Connection con = DriverManager.getConnection(connectionUrl);
}
catch (SQLException e)
{
System.out.println(“SQL Exception: “+ e.toString());
}
catch (ClassNotFoundException cE)
{
System.out.println(“Class Not Found Exception: “+ cE.toString());
}
Hi, Sitti
First, I thought that your MySQL is on remote machine. So if it on the local machine, I suggest you to change the connection string “localhost” to your real ip address of the machine.
Also, have you forgot username and password on the connection string?
hey the records are getting inserted in table properly,but when i am trying to retrieve a single row i am getting a blank page. when i retrieve da whole table it is retrieving… but for one particular row it is not….
my query is : “select * from stu_detail where std_rollno=+ num +”
( String num=request.getParameter(“stdrollnumber”);
when i enter student roll number i am supposed to get student name,father’s name,address and contact no in the same page inside the textboxes.
Hi, Halima
I suggest you print out your query on output window and copy it to run on SQL Server directly to see if there is any error or not.
Thank linglom.. great help… I’m able to start working on it.. ♥
hey i tried in other system ,its working perfectly but i dont know wat’s da error….
plz help me with other query to retrieve a record , whatever user selects…
hey i retrieved all da records from the database in an table(i hav 5 columns i.e,- rollnumber,name,father’s name,address,ph).I have made da rollnumber as an link, so when i click on one particular rollnumber i should get all da columns of dat record in an table so dat i can edit and update it using servlet…
How to do it….???
Hello Friends Feels so good to be at a place of my interest where a hope that someone can help:
I am using netbeans for my project and mysql database to create a hash table for my web search engine project
the program runs fine the Problem: is Where does the actual physical data of the mysql table is stored.
I can locate the .frm files of the table but there needs to be an .Myd file also where does it get stored??
////////////please help anyone////////////
Linglom, you really did well with the way you explained this. I was able to even apply it to my JApplet in Netbeans and the part where I had to add MySQL Driver to the library made the difference.
I’d thought of using this information for my Java web development in Netbeans, but although the applet was running without any error, I kept getting a “ClassNotFoundException” error message.
What I did was to use the <jsp:plugin … to insert the applet
Please, give your counsel. Thank you.
…
Applet compiled and was executed without any error, but when I used the tag to insert it into the web page, a “ClassNotFoundException” error message was generated and the part where the statement:
==>Class.forName(“com.mysql.jdbc.Driver”);<==
was made never got executed.
—
Just explaining better… Thank you.
THnX a LOT MAn…..REalLY HelPFUl ……..
GOd BlESS YoU..
THnX a LOT MAn…..REalLY HelPFUl ……..
great!
Thanks for the info.
It is simple and superb!
Thanks a lot for this. It was simple highly useful.
thank youuuuuuuuuuu….soooooooooooooo much…..there was great difficulty in update query…..you solved the problem…
Just like all the previous Posts…THANK YOU!!! Took a lot of stress of my shoulders!!
I created a java desktop application that accept value for name,address and city and i have a button which will submit the information to the database, i need the code i will put in the action listener of the button that will submit all the data to mysql database
hi! can u send me a simple login program in jsp with mysql with validations and login failed msg if given wrong username and password.im able to establish a connection string but it goes to the next page even when i give wrong user name and password though i ve created a user table with username and password data. thank you !!!
hey! i need to write a query to delete a record in the mysql table using java.
this is the command:
try{
Class.forName(“java.sql.Driver”);
Connection c=DriverManager.getConnection(“jdbc:mysql://localhost/hrishi”,”root”,”mysqldb”);
Statement s=c.createStatement();
ResultSet r=s.executeQuery(“delete from shop where num=18;”);
while(r.next()){
mnc.removeRow(mdl);
dlm1.remove(mdl);
}
c.close();
s.close();
r.close();
} catch(Exception e){
System.out.println(“Error”);
}
can u please tell me if theres an error or if there s somethin i need to add? Thank you.
this tutorial is useful. thanks a lot.
Thankx 🙂
Thanks very much it has been very helpful for me… Love YOU!!!!
Thanks a lot
I had spend a lot of time for this purpose . But it help
me very much !!!!!!!!!!
hey! i want to know the syntax if we have to retrive the data from table for a particular record. i m using netbeans for desktop app,in my app i entered value in field and after click on a button the required data is found. i m making app of employee record. means dat when i entered the emp_id then particular data of employee is found……….
“SELECT* From emp where id=…..”
what should i write in blank?
Can anyone help me out by sending the query for an login form in mysql
can anyone help me out. i created a database on my system and accesd it with java but how can i transfer the database to another system and still access it …… it always gives me an error message
I have read several just right stuff here. Definitely worth bookmarking for revisiting. I surprise how a lot effort you put to create the sort of excellent informative site.
Hello,
I want to add Search(mysql query) for a JButton and result should be shown in JTable . how can i do that ??? (I’m using NetBeans)
I have xampp mysql DB set on my pc. I want to perform ETL on a table from different machine. How can this be achieved ? Please put some light on this.
Thanks in advance.
My Source:
Imported:
import javax.swing.JOptionPane;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
code used for insertion:
try{
Class.forName(“java.sql.Driver”);
Connection con = DriverManager.getConnection(“jdbc:mysql://localhost:3306/ebay”,”root”,”voxrazr”);
Statement stmt = con.createStatement();
String query = “INSERT INTO user_data VALUES (”
+fname.getText()
+”‘”+lname.getText()+”‘”
+”‘”+s_add.getText()+”‘”
+”‘”+city.getText()+”‘”
+”‘”+state.getText()+”‘”
+”‘”+pcode.getText()+”‘”
+”‘”+ptno.getText()+”‘”
+”‘”+email.getText()+”‘”
+”‘”+userid.getText()+”‘”
+”‘”+pass.getText()+”‘”
+”‘”+month.getSelectedItem()+”‘”
+”‘”+day.getSelectedItem()+”‘”
+”‘”+year.getSelectedItem()+”‘”
+”‘”+country.getSelectedItem()+”‘”+”);”;
ResultSet rs = stmt.executeUpdate(query);
}
catch(Exception e){
JOptionPane.showMessageDialog(null,”Error in connectivity”);
}
i got an error in:
ResultSet rs = stmt.executeUpdate(query);
output:
init:
deps-clean:
Updating property file: C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\build\built-clean.properties
Deleting directory C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\build
clean:
init:
deps-jar:
Created dir: C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\build
Updating property file: C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\build\built-jar.properties
Created dir: C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\build\classes
Created dir: C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\build\empty
Created dir: C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\build\generated-sources\ap-source-output
Compiling 1 source file to C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\build\classes
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3088: incompatible types
found : int
required: java.sql.ResultSet
ResultSet rs = stmt.executeUpdate(query);
1 error
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\nbproject\build-impl.xml:603: The following error occurred while executing this line:
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\nbproject\build-impl.xml:245: Compile failed; see the compiler error output for details.
BUILD FAILED (total time: 2 seconds)
Please help me to remove this error.
@Sahil:
ResultSet rs = stmt.executeUpdate(query);
Above code doesn’t return a ResultSet type but it returns an integer value as follows :
Return value:-
either the row count for SQL Data Manipulation Language (DML) statements or 0 for SQL statements that return nothing
So the code should be as follows : “stmt.executeUpdate(query); ” and not “ResultSet rs = stmt.executeUpdate(query); “
@ap:
The error is gone thanx for that. but still i didn’t get the desired result.Here i am showing you the code i used in MySQL:
CREATE DATABASE ebay;
USE eBay;
CREATE TABLE user_data
( fname CHAR(10),
lname CHAR(10),
strt_adrs VARCHAR(50),
city CHAR(15),
s_p_r CHAR(25),
pcode INTEGER,
pri_tele_no INTEGER,
eid VARCHAR(50),
userid VARCHAR(50),
password VARCHAR(50),
month CHAR(15),
day INTEGER,
year INTEGER );
ALTER TABLE user_data
ADD(country VARCHAR(20));
I wan’t to insert the data into it in a single click.
@Sahil : What error are you getting and on which line ?
The insert query should be somewhat like this..(i think), just try it out once :
“INSERT INTO user_data VALUES (‒
+fname.getText()+‒,’â€
+lname.getText()+‒,’â€
+s_add.getText()+‒,’â€
+city.getText()+‒,’â€
+state.getText()+‒,’â€
+pcode.getText()+‒,’â€
+ptno.getText()+‒,’â€
+email.getText()+‒,’â€
+userid.getText()+‒,’â€
+pass.getText()+‒,’â€
+month.getSelectedItem()+‒,’â€
+day.getSelectedItem()+‒,’â€
+year.getSelectedItem()+‒,’â€
+country.getSelectedItem()+‒);â€
Sorry about saying but when i put that my all code is showing error.
@Sahil :
Firstly, where are you writing this code, is it eclipse or netbeans or other.
and when you say u r getting errors do post the errors also.
NetBeans. And the following error i got when i used your code:
init:
deps-clean:
Updating property file: C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\build\built-clean.properties
Deleting directory C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\build
clean:
init:
deps-jar:
Created dir: C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\build
Updating property file: C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\build\built-jar.properties
Created dir: C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\build\classes
Created dir: C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\build\empty
Created dir: C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\build\generated-sources\ap-source-output
Compiling 1 source file to C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\build\classes
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3073: illegal character: \8220
String query = “INSERT INTO user_data VALUES (â€â€™
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3073: ‘;’ expected
String query = “INSERT INTO user_data VALUES (â€â€™
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3073: ‘;’ expected
String query = “INSERT INTO user_data VALUES (â€â€™
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3073: illegal character: \8221
String query = “INSERT INTO user_data VALUES (â€â€™
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3073: illegal character: \8217
String query = “INSERT INTO user_data VALUES (â€â€™
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3074: illegal character: \8221
+fname.getText()+â€â€™,’â€
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3074: not a statement
+fname.getText()+â€â€™,’â€
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3074: illegal character: \8217
+fname.getText()+â€â€™,’â€
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3074: ‘;’ expected
+fname.getText()+â€â€™,’â€
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3074: unclosed character literal
+fname.getText()+â€â€™,’â€
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3075: illegal character: \8221
+lname.getText()+â€â€™,’â€
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3075: not a statement
+lname.getText()+â€â€™,’â€
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3075: illegal character: \8217
+lname.getText()+â€â€™,’â€
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3075: ‘;’ expected
+lname.getText()+â€â€™,’â€
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3075: unclosed character literal
+lname.getText()+â€â€™,’â€
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3076: illegal character: \8221
+s_add.getText()+â€â€™,’â€
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3076: not a statement
+s_add.getText()+â€â€™,’â€
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3076: illegal character: \8217
+s_add.getText()+â€â€™,’â€
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3076: ‘;’ expected
+s_add.getText()+â€â€™,’â€
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3076: unclosed character literal
+s_add.getText()+â€â€™,’â€
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3077: illegal character: \8221
+city.getText()+â€â€™,’â€
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3077: not a statement
+city.getText()+â€â€™,’â€
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3077: illegal character: \8217
+city.getText()+â€â€™,’â€
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3077: ‘;’ expected
+city.getText()+â€â€™,’â€
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3077: unclosed character literal
+city.getText()+â€â€™,’â€
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3078: illegal character: \8221
+state.getText()+â€â€™,’â€
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3078: not a statement
+state.getText()+â€â€™,’â€
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3078: illegal character: \8217
+state.getText()+â€â€™,’â€
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3078: ‘;’ expected
+state.getText()+â€â€™,’â€
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3078: unclosed character literal
+state.getText()+â€â€™,’â€
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3079: illegal character: \8221
+pcode.getText()+â€â€™,’â€
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3079: not a statement
+pcode.getText()+â€â€™,’â€
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3079: illegal character: \8217
+pcode.getText()+â€â€™,’â€
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3079: ‘;’ expected
+pcode.getText()+â€â€™,’â€
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3079: unclosed character literal
+pcode.getText()+â€â€™,’â€
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3080: illegal character: \8221
+ptno.getText()+â€â€™,’â€
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3080: not a statement
+ptno.getText()+â€â€™,’â€
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3080: illegal character: \8217
+ptno.getText()+â€â€™,’â€
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3080: ‘;’ expected
+ptno.getText()+â€â€™,’â€
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3080: unclosed character literal
+ptno.getText()+â€â€™,’â€
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3081: illegal character: \8221
+email.getText()+â€â€™,’â€
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3081: not a statement
+email.getText()+â€â€™,’â€
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3081: illegal character: \8217
+email.getText()+â€â€™,’â€
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3081: ‘;’ expected
+email.getText()+â€â€™,’â€
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3081: unclosed character literal
+email.getText()+â€â€™,’â€
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3082: illegal character: \8221
+userid.getText()+â€â€™,’â€
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3082: not a statement
+userid.getText()+â€â€™,’â€
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3082: illegal character: \8217
+userid.getText()+â€â€™,’â€
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3082: ‘;’ expected
+userid.getText()+â€â€™,’â€
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3082: unclosed character literal
+userid.getText()+â€â€™,’â€
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3083: illegal character: \8221
+pass.getText()+â€â€™,’â€
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3083: not a statement
+pass.getText()+â€â€™,’â€
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3083: illegal character: \8217
+pass.getText()+â€â€™,’â€
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3083: ‘;’ expected
+pass.getText()+â€â€™,’â€
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3083: unclosed character literal
+pass.getText()+â€â€™,’â€
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3084: illegal character: \8221
+month.getSelectedItem()+â€â€™,’â€
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3084: not a statement
+month.getSelectedItem()+â€â€™,’â€
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3084: illegal character: \8217
+month.getSelectedItem()+â€â€™,’â€
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3084: ‘;’ expected
+month.getSelectedItem()+â€â€™,’â€
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3084: unclosed character literal
+month.getSelectedItem()+â€â€™,’â€
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3085: illegal character: \8221
+day.getSelectedItem()+â€â€™,’â€
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3085: not a statement
+day.getSelectedItem()+â€â€™,’â€
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3085: illegal character: \8217
+day.getSelectedItem()+â€â€™,’â€
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3085: ‘;’ expected
+day.getSelectedItem()+â€â€™,’â€
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3085: unclosed character literal
+day.getSelectedItem()+â€â€™,’â€
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3086: illegal character: \8221
+year.getSelectedItem()+â€â€™,’â€
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3086: not a statement
+year.getSelectedItem()+â€â€™,’â€
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3086: illegal character: \8217
+year.getSelectedItem()+â€â€™,’â€
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3086: ‘;’ expected
+year.getSelectedItem()+â€â€™,’â€
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3086: unclosed character literal
+year.getSelectedItem()+â€â€™,’â€
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3087: illegal character: \8221
+country.getSelectedItem()+â€â€™);â€
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3087: not a statement
+country.getSelectedItem()+â€â€™);â€
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3087: illegal character: \8217
+country.getSelectedItem()+â€â€™);â€
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3087: ‘;’ expected
+country.getSelectedItem()+â€â€™);â€
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\src\ebay.java:3087: illegal character: \8221
+country.getSelectedItem()+â€â€™);â€
75 errors
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\nbproject\build-impl.xml:603: The following error occurred while executing this line:
C:\Users\VOXsaga\Documents\NetBeansProjects\ebay\nbproject\build-impl.xml:245: Compile failed; see the compiler error output for details.
BUILD FAILED (total time: 4 seconds)
@Sahil:
k…
First try adding a simple entry like this in Netbeans:
statment.executeUpdate(“Insert into user_data(fname,lname,s_add) values(‘ABC’,’XYZ’,’India’)”);
and let me know if this works…
Reply.
@ap:
For that i made a new database and a new project in netbeans.
Database code:
CREATE database Try;
USE Try;
CREATE TABLE user_data
(fname VARCHAR(20),
lname VARCHAR(20),
s_add VARCHAR(30));
NetBeans:
Project Name: try
Frame Name: try_1
+————+—————+—————-+
+ Control + Variable Name + Caption +
+————+—————+—————-+
+ Label + ————- + First Name +
+ Label + ————- + Last Name +
+ Label + ————- + Street Address +
+ Text Field + fn + ————– +
+ Text Field + ln + ————– +
+ Text Field + str_add + ————– +
+ Button + ————- + REGISTER +
+————+—————+—————-+
Source:
Import Commands:
import java.sql.*;
import javax.swing.JOptionPane;
Code:
try{
Class.forName(“java.sql.Driver”);
Connection con = DriverManager.getConnection(“jdbc:mysql://localhost:3306/Try”,”root”,”voxrazr”);
Statement stmt = con.createStatement();
statment.executeUpdate(“Insert into user_data(fname,lname,s_add) values(‘ABC’,’XYZ’,’India’)â€);
}
catch(Exception e){
JOptionPane.showMessageDialog(null,”Error in connectivity”);
}
Error:
init:
deps-clean:
Updating property file: C:\Users\VOXsaga\Documents\NetBeansProjects\try\build\built-clean.properties
Deleting directory C:\Users\VOXsaga\Documents\NetBeansProjects\try\build
clean:
init:
deps-jar:
Created dir: C:\Users\VOXsaga\Documents\NetBeansProjects\try\build
Updating property file: C:\Users\VOXsaga\Documents\NetBeansProjects\try\build\built-jar.properties
Created dir: C:\Users\VOXsaga\Documents\NetBeansProjects\try\build\classes
Created dir: C:\Users\VOXsaga\Documents\NetBeansProjects\try\build\empty
Created dir: C:\Users\VOXsaga\Documents\NetBeansProjects\try\build\generated-sources\ap-source-output
Compiling 1 source file to C:\Users\VOXsaga\Documents\NetBeansProjects\try\build\classes
C:\Users\VOXsaga\Documents\NetBeansProjects\try\src\try_1.java:105: illegal character: \8220
statment.executeUpdate(“Insert into user_data(fname,lname,s_add) values(‘ABC’,’XYZ’,’India’)â€);
C:\Users\VOXsaga\Documents\NetBeansProjects\try\src\try_1.java:105: ‘;’ expected
statment.executeUpdate(“Insert into user_data(fname,lname,s_add) values(‘ABC’,’XYZ’,’India’)â€);
C:\Users\VOXsaga\Documents\NetBeansProjects\try\src\try_1.java:105: ‘;’ expected
statment.executeUpdate(“Insert into user_data(fname,lname,s_add) values(‘ABC’,’XYZ’,’India’)â€);
C:\Users\VOXsaga\Documents\NetBeansProjects\try\src\try_1.java:105: not a statement
statment.executeUpdate(“Insert into user_data(fname,lname,s_add) values(‘ABC’,’XYZ’,’India’)â€);
C:\Users\VOXsaga\Documents\NetBeansProjects\try\src\try_1.java:105: ‘;’ expected
statment.executeUpdate(“Insert into user_data(fname,lname,s_add) values(‘ABC’,’XYZ’,’India’)â€);
C:\Users\VOXsaga\Documents\NetBeansProjects\try\src\try_1.java:105: not a statement
statment.executeUpdate(“Insert into user_data(fname,lname,s_add) values(‘ABC’,’XYZ’,’India’)â€);
C:\Users\VOXsaga\Documents\NetBeansProjects\try\src\try_1.java:105: ‘;’ expected
statment.executeUpdate(“Insert into user_data(fname,lname,s_add) values(‘ABC’,’XYZ’,’India’)â€);
C:\Users\VOXsaga\Documents\NetBeansProjects\try\src\try_1.java:105: not a statement
statment.executeUpdate(“Insert into user_data(fname,lname,s_add) values(‘ABC’,’XYZ’,’India’)â€);
C:\Users\VOXsaga\Documents\NetBeansProjects\try\src\try_1.java:105: ‘;’ expected
statment.executeUpdate(“Insert into user_data(fname,lname,s_add) values(‘ABC’,’XYZ’,’India’)â€);
C:\Users\VOXsaga\Documents\NetBeansProjects\try\src\try_1.java:105: illegal character: \8216
statment.executeUpdate(“Insert into user_data(fname,lname,s_add) values(‘ABC’,’XYZ’,’India’)â€);
C:\Users\VOXsaga\Documents\NetBeansProjects\try\src\try_1.java:105: ‘;’ expected
statment.executeUpdate(“Insert into user_data(fname,lname,s_add) values(‘ABC’,’XYZ’,’India’)â€);
C:\Users\VOXsaga\Documents\NetBeansProjects\try\src\try_1.java:105: illegal character: \8217
statment.executeUpdate(“Insert into user_data(fname,lname,s_add) values(‘ABC’,’XYZ’,’India’)â€);
C:\Users\VOXsaga\Documents\NetBeansProjects\try\src\try_1.java:105: ‘;’ expected
statment.executeUpdate(“Insert into user_data(fname,lname,s_add) values(‘ABC’,’XYZ’,’India’)â€);
C:\Users\VOXsaga\Documents\NetBeansProjects\try\src\try_1.java:105: unclosed character literal
statment.executeUpdate(“Insert into user_data(fname,lname,s_add) values(‘ABC’,’XYZ’,’India’)â€);
C:\Users\VOXsaga\Documents\NetBeansProjects\try\src\try_1.java:105: illegal character: \8217
statment.executeUpdate(“Insert into user_data(fname,lname,s_add) values(‘ABC’,’XYZ’,’India’)â€);
C:\Users\VOXsaga\Documents\NetBeansProjects\try\src\try_1.java:105: unclosed character literal
statment.executeUpdate(“Insert into user_data(fname,lname,s_add) values(‘ABC’,’XYZ’,’India’)â€);
C:\Users\VOXsaga\Documents\NetBeansProjects\try\src\try_1.java:105: illegal character: \8217
statment.executeUpdate(“Insert into user_data(fname,lname,s_add) values(‘ABC’,’XYZ’,’India’)â€);
C:\Users\VOXsaga\Documents\NetBeansProjects\try\src\try_1.java:105: not a statement
statment.executeUpdate(“Insert into user_data(fname,lname,s_add) values(‘ABC’,’XYZ’,’India’)â€);
C:\Users\VOXsaga\Documents\NetBeansProjects\try\src\try_1.java:105: illegal character: \8221
statment.executeUpdate(“Insert into user_data(fname,lname,s_add) values(‘ABC’,’XYZ’,’India’)â€);
19 errors
C:\Users\VOXsaga\Documents\NetBeansProjects\try\nbproject\build-impl.xml:603: The following error occurred while executing this line:
C:\Users\VOXsaga\Documents\NetBeansProjects\try\nbproject\build-impl.xml:245: Compile failed; see the compiler error output for details.
BUILD FAILED (total time: 1 second)
+————+—————–+————————-+—————-+
+ Control + Variable Name + Caption +
+————+—————–+————————-+—————-+
+ Label + ——————— + First Name +
+ Label + ——————— + Last Name +
+ Label + ——————— + Street Address +
+ Text Field + fn + ————–——— +
+ Text Field + ln + ————–——— +
+ Text Field + str_add + ————–——— +
+ Button + ——————— + REGISTER +
+——————–+————-————+———–—————+
@Sahil:
Just write a simple java program which shows entries from a table in DB without the GUI. And then include the code in GUI based program.
@sAHIL:
U can use this code to try out as said earlier.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class select {
public static void main(String args[]) throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException{
Connection con=null;
Statement s=null;
ResultSet rs=null;
//DB connection
Class.forName(“com.mysql.jdbc.Driver”).newInstance();
con = DriverManager.getConnection(“jdbc:mysql://localhost/”,”root”,””);
System.out.println(“Connection Success!!”);
s = con.createStatement();
String query = “select * from “;
rs = s.executeQuery(query);
if(rs.next()) {
System.out.println(rs.getString(1) + “\t\t” + rs.getString(2));
}
}
}
@sAHIL:
iGNORE the earlier comment.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class select {
public static void main(String args[]) throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException{
Connection con=null;
Statement s=null;
ResultSet rs=null;
//DB connection
Class.forName(“com.mysql.jdbc.Driverâ€).newInstance();
con = DriverManager.getConnection(“jdbc:mysql://localhost/DB-NAMEâ€,â€rootâ€,—);
System.out.println(“Connection Success!!â€);
s = con.createStatement();
String query = “select * from TABLE-NAME“;
rs = s.executeQuery(query);
if(rs.next()) {
System.out.println(rs.getString(1) + “\t\t†+ rs.getString(2));
}
}
}
can you give it according to my frame. cause i am weak in netbeans. The code given by you is showing error in my project ‘try’ which i had commented earlier.
@Sahil :
just create a new java file named “select.java” and paste the above code in it.
Also make the changes in code for the one’s in CAPITAL.
i.e. : TABLE-NAME :- Enter the table from which you want to retrieve details and DB-NAME :- whichever database the above table lies in.
Thank you. Your explanations helped me a lot. And everything worked perfectly. 🙂
I tried to run this code in Netbeans, and it was successfully built up but i could not print out in console, it means i got result as
run:
BUILD SUCCESSFUL (total time: 0 seconds)
is there anyone who can help me to solve this problem…
package database;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class Database {
public static void main(String[] args) {
try
{
Connection con;
Statement st;
ResultSet rs;
Class.forName(“com.mysql.jdbc.Driver”);
con = DriverManager.getConnection(“jdbc:mysql//localhost/database”, “root”, “”);
System.out.println (“Connected to Database”);
st = con.createStatement();
String sql = ” select * from authors”;
rs = st.executeQuery(sql);
while (rs.next())
{
String authorID = rs.getString(“AUTHOR_ID”);
String firstName = rs.getString (“FIRSTNAME”);
String lastName = rs.getString (“‘LASTNAME'”);
System.out.println (authorID + “:”+firstName+””+lastName);
}
}
catch (Exception e)
{
}
}
}
good tutorial.
How to insert records in the button click event? i have already established the connection in the main method.
thank for your help what a nice tut