Skip to main content

jdbc mysql example in java

Here’s an example to show you how to connect to MySQL database via a JDBC driver. To connect to MySQL from Java, you have to use the JDBC driver from MySQL. You find the latest MySQL JDBC driver under the following URL:
Create a example database and named it test. Now create a a table name it user. Create table user ((userid INT NOT NULL AUTO_INCREMENT,
username VARCHAR(30) NOT NULL,
     PRIMARY KEY (userid));
 Now add the record in your table;
 Now create a project in eclipse and mysql-connector jar in its classpath.
package com.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
publicclass JavaJDBCExample {
private Connection connect = null;
private Statement statement = null;
private ResultSet resultSet = null;
publicvoid readDatabase()
try {
// this will load the MySQL driver
// setup the connection with the database.
connect = DriverManager
+ "user=root&password=admin");
// statements allow to issue SQL queries to the database
statement = connect.createStatement();
// resultSet gets the result of the SQL query
resultSet = statement
.executeQuery("select * from user");
//Retrieve by column name
int id = resultSet.getInt("userid");
String username = resultSet.getString("username");
System.out.print("userid: " + id);
System.out.print(", username: " + username+"\n");
//STEP 6: Clean-up environment
} catch (Exception e) {
} finally {
try {
} catch (SQLException e) {
// TODO Auto-generated catch block
publicstaticvoid main(String[] args) {
// TODO Auto-generated method stub
new JavaJDBCExample().readDatabase();

Now run the java file and see the result.


Popular posts from this blog

lucene example

In simple words: it parses a text and without saving the text the framework can search inside it. It keeps track of terms used inside the text. Of course you can optionally save the text into your <a href="" data-cke-saved-href="">lucene index</a>.

 Lets take an example.

 I have created a database and a table named it 'city'.

 create table `city` (

 `id` bigint (20),

 `city_name` varchar (765)


 Now I have added two record Kolkata,Delhi in the database.

 Now create a java project and add lucene-analyzers-common-4.0.0.jar,lucene-core-4.0.0.jar,lucene-queries-4.0.0.jar,lucene-queryparser-4.0.0.jar and mysql-connector-java-5.1.22-bin.jar in the project.

 create a java class named LuceneTest  and write the following.

LuceneTest .java

 package com.test;

singleton design pattern

Java Singleton design pattern is one of the design pattern which suggest that only one instance of a Singleton object is created by the JVM. This is useful when exactly one object is needed to coordinate actions across the system Example: public class Singleton{ privatestatic Singleton singleton = null; private Singleton(){ } publicstaticsynchronized Singleton getInstance( ) { if(singleton == null) { singleton = new Singleton(); } returnsingleton; } publicvoid test( ) { System.out.println("test"); } @Override protected Object clone() throws CloneNotSupportedException { // TODO Auto-generated method stub returnnew CloneNotSupportedException(); } } publicclass SingletonTest { publicstaticvoid main(String[] args){ Singleton singleton = Singleton.getInstance(); try{ Singleton