Beruflich Dokumente
Kultur Dokumente
Steps
There are seven standard steps in querying databases:
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:
@localhost: 1521:xe",
"system","password");
Statement stmt=con.createStatement();
while(rs.next()){
System.out.println(rs.getInt(1)+" "+rs.getString(2));
}
con.close();
try {
Class.forName("connect.microsoft.MicrosoftDriver") ;
//Class.forName("oracle.jdbc.driver.OracleDriver"); for Oracle driver
//Class.forName("com.sybase.jdbc.SybDriver"); for sybase driver
} catch(ClassNotFoundException e) {
System.err.println("Error loading driver: " + e);
}
prepareStatement
prepareCall
rollback
commit
close
isClosed
*********************************
Example to connect to the mysql database in java
For connecting java application with the mysql database, you need to follow 5
steps to perform database connectivity.
Let's first create a table in the mysql database, but before creating table, we
need to create database first.
In this example, sonoo is the database name, root is the username and
password.
import java.sql.*;
class MysqlCon{
public static void main(String args[]){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:3306/sonoo","root","root");
//here sonoo is database name, root is username and password
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from emp");
while(rs.next())
System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3));
con.close();
}catch(Exception e){ System.out.println(e);}
}
}
The above example will fetch all the records of emp table.
2) set classpath:
There are two ways to set the classpath:
temporary
permanent
C:>set classpath=c:\folder\mysql-connector-java-5.0.8-bin.jar;.;
// Database credentials
// static final String USER = "username";
static final String PASS = "root";
//Display values
System.out.print("ID: " + id);
System.out.print(", Age: " + age);
System.out.print(", First: " + first);
System.out.println(", Last: " + last);
}
//STEP 6: Clean-up environment
}
stmt.close();
conn.close();
}catch(SQLException se){
//Handle errors for JDBC
se.printStackTrace();
}catch(Exception e){
//Handle errors for Class.forName
e.printStackTrace();
}finally{
//finally block used to close resources
try{
if(stmt!=null)
stmt.close();
}catch(SQLException se2){
}// nothing we can do
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}//end finally try
}//end try
System.out.println("Goodbye!");
}//end main
}//end FirstExample
*******************************************
//JDBCExample.java
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println("Where is your MySQL JDBC Driver?");
e.printStackTrace();
return;
}
} catch (SQLException e) {
System.out.println("Connection Failed! Check output console");
e.printStackTrace();
return;
}
if (connection != null) {
System.out.println("You made it, take control your database
now!");
} else {
System.out.println("Failed to make connection!");
}
}
}
***************************************************
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
try {
// connect way #1
String url1 = "jdbc:mysql://localhost:3306/test1";
String user = "";
String password = "root";
// connect way #2
String url2 = "jdbc:mysql://localhost:3306/test2?
user=root&password=secret";
conn2 = DriverManager.getConnection(url2);
if (conn2 != null) {
System.out.println("Connected to the database test2");
}
// connect way #3
String url3 = "jdbc:mysql://localhost:3306/test3";
Properties info = new Properties();
info.put("user", "");
info.put("password", "root");
String connectionURL =
"jdbc:postgresql://localhost:5432/movies;user=java;password=samples";
// Change the connection string according to your db, ip, username and
password
try {
Introduction
This article explains how to search user records from a database by their
name in the Swing GUI of Java. The NetBeans IDE is used to create the
sample examples.
Searching Records from Database in a Windows Forms form using Swing GUI
1. SwingSearchApp.java
This Java file consists of the entire logic. First of all we initialize the JFrame
components using a constructor then create a database connection and
finally set the database value to the textfield. If the given name is not found
in the database then it displays an error message and displays it by running
the constructor.
2. emp.sql
For fetching records we need a database table; for that we create an "emp"
table in our database.
Syntax
emp.sql
Now let's start creating this app. Use the following procedure to do that in the
NetBeans IDE.
Step 1
Step 2
Java Application
Step 3
SwingSearchApp
Step 4
SwingSearchApp.java
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.sql.*;
//Initializing Components
JButton btn;
//Providing Title
btn.addActionListener(this);
lb.setForeground(Color.red);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 500);
setLayout(null);
add(lb5);
add(tf5);
add(btn);
add(lb);
add(lb1);
add(tf1);
add(lb2);
add(tf2);
add(lb3);
add(tf3);
add(lb4);
add(tf4);
tf1.setEditable(false);
tf2.setEditable(false);
tf3.setEditable(false);
tf4.setEditable(false);
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con =
DriverManager.getConnection("jdbc:oracle:thin:@mcndesktop07:1521",
"sandeep", "welcome");
st.setString(1, str);
//Excuting Query
ResultSet rs = st.executeQuery();
if (rs.next()) {
String s = rs.getString(1);
String s1 = rs.getString(2);
String s2 = rs.getString(3);
String s3 = rs.getString(4);
tf1.setText(s);
tf2.setText(s1);
tf3.setText(s2);
tf4.setText(s3);
} else {
System.out.println(ex);
//Running Constructor
new SwingSearchApp();
}
}
***************************************