package Queries;
import java.util.Scanner;
import java.sql.*;
public class InsertRoom {
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DATABASE_URL = "jdbc:mysql://localhost/doscstschedule";
static private PreparedStatement insertNewRoom = null;
static String roomCode;
static String deptCode;
public static void main(String[] args) {
Connection connection = null; // manages connection
// connect to database doscstschedule and query database
try {
Class.forName(JDBC_DRIVER); // load database driver class
// establish connection to database
connection = DriverManager.getConnection(DATABASE_URL, "root", "");
Scanner input = new Scanner(System.in);
System.out.print("Enter room code: ");
roomCode = input.next();
System.out.print("Enter department code: ");
deptCode = input.next();
// create Statement for querying database
insertNewRoom = connection.prepareStatement("insert into room values(?,?)");
insertNewRoom.setString(1, roomCode);
insertNewRoom.setString(2, deptCode);
insertNewRoom.executeUpdate();
System.out.println("Data successfully added");
} // end try
catch (SQLException sqlException) {
sqlException.getMessage();
System.exit(0);
} // end catch
catch (ClassNotFoundException classNotFound) {
classNotFound.getMessage();
System.exit(0);
} // end catch
finally // ensure statement and connection are closed properly
{
try {
insertNewRoom.close();
connection.close();
} // end try
catch (Exception exception) {
exception.getMessage();
System.exit(0);
} // end catch
} // end finally
}
}
Information Technology, Discussion, IT Subject, and Exploration
Thursday, January 21, 2010
Friday, January 15, 2010
Displaying data in database using java and mySQL
//This is only an example program on displaying data from database using java and mysql
//important information should be considered.
import java.sql.*;
public class DisplaySubject {
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DATABASE_URL = "jdbc:mysql://localhost/doscstschedule";
public static void main(String[] args) {
Connection connection = null; // manages connection
Statement statement = null; // query statement
// connect to database books and query database
try {
Class.forName(JDBC_DRIVER); // load database driver class
// establish connection to database
connection =
DriverManager.getConnection(DATABASE_URL, "root","");
// create Statement for querying database
statement = connection.createStatement();
// query database
ResultSet resultSet = statement.executeQuery(
"SELECT subjCode,subjDesc, lecUnits, labUnits FROM subject");
// process query results
ResultSetMetaData metaData = resultSet.getMetaData();
int numberOfColumns = metaData.getColumnCount();
System.out.println("Subject Table of DOSCST Class Schedule Database:");
for (int i = 1; i <= numberOfColumns; i++) {
System.out.printf("%-30s\t", metaData.getColumnName(i));
}
System.out.println();
while (resultSet.next()) {
for (int i = 1; i <= numberOfColumns; i++) {
System.out.printf("%-30s\t", resultSet.getObject(i));
}
System.out.println();
} // end while
} // end try
catch (SQLException sqlException) {
sqlException.printStackTrace();
System.exit(1);
} // end catch
catch (ClassNotFoundException classNotFound) {
classNotFound.printStackTrace();
System.exit(1);
} // end catch
finally // ensure statement and connection are closed properly
{
try {
statement.close();
connection.close();
} // end try
catch (Exception exception) {
exception.printStackTrace();
System.exit(1);
} // end catch
} // end finall
}
}
//important information should be considered.
import java.sql.*;
public class DisplaySubject {
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DATABASE_URL = "jdbc:mysql://localhost/doscstschedule";
public static void main(String[] args) {
Connection connection = null; // manages connection
Statement statement = null; // query statement
// connect to database books and query database
try {
Class.forName(JDBC_DRIVER); // load database driver class
// establish connection to database
connection =
DriverManager.getConnection(DATABASE_URL, "root","");
// create Statement for querying database
statement = connection.createStatement();
// query database
ResultSet resultSet = statement.executeQuery(
"SELECT subjCode,subjDesc, lecUnits, labUnits FROM subject");
// process query results
ResultSetMetaData metaData = resultSet.getMetaData();
int numberOfColumns = metaData.getColumnCount();
System.out.println("Subject Table of DOSCST Class Schedule Database:");
for (int i = 1; i <= numberOfColumns; i++) {
System.out.printf("%-30s\t", metaData.getColumnName(i));
}
System.out.println();
while (resultSet.next()) {
for (int i = 1; i <= numberOfColumns; i++) {
System.out.printf("%-30s\t", resultSet.getObject(i));
}
System.out.println();
} // end while
} // end try
catch (SQLException sqlException) {
sqlException.printStackTrace();
System.exit(1);
} // end catch
catch (ClassNotFoundException classNotFound) {
classNotFound.printStackTrace();
System.exit(1);
} // end catch
finally // ensure statement and connection are closed properly
{
try {
statement.close();
connection.close();
} // end try
catch (Exception exception) {
exception.printStackTrace();
System.exit(1);
} // end catch
} // end finall
}
}
Tuesday, January 12, 2010
Java JDBC using MySQL Database
Important notes:
import java.sql.*;
import javax.swing.JOptionPane;
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws Exception {
StringBuffer output = new StringBuffer();
output.append("Tesing MySQL Driver installation");
try {
String className = "com.mysql.jdbc.Driver";
Class driverObject = Class.forName(className);
output.append("Driver: "+driverObject+"\n");
output.append("Driver Installation Successful");
JOptionPane.showMessageDialog(null, output);
}
catch(Exception x){
output = new StringBuffer();
output.append("Driver Installation FAILED\n");
JOptionPane.showMessageDialog(null,output);
System.out.println("Failed Driver Error:" +x.getMessage());
}
}
}
* on this part is an example of how to connect the mysql database using java
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.SQLException;
public class JDBCDriverInformation {
static String userid = "root";
static String password = "";
static String url = "jdbc:mysql://localhost:3306/databaseapp";
static Connection con = null;
static String className = "com.mysql.jdbc.Driver";
public static void main(String[] args) throws Exception {
Connection con = getMySQLJDBCConnection();
if (con != null) {
System.out.println("Got Connection");
DatabaseMetaData meta = con.getMetaData();
System.out.println("Driver Name : " + meta.getDriverName());
System.out.println("Driver Version : " + meta.getDriverVersion());
} else {
System.out.println("Could not get connection");
}
}
public static Connection getMySQLJDBCConnection() {
try {
Class.forName(className);
} catch (java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}
try {
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/databaseapp", userid, password);
} catch (SQLException ex) {
System.err.println("SQLException: " + ex.getMessage());
}
return con;
}
}
Hope you can run successfully in this given example.
- You need to download the jdbc connector of mysql and java in http://dev.mysql.com/downloads/connector/j/
- Add this package to your libraries
- The information below is all about how to access the driver.
import java.sql.*;
import javax.swing.JOptionPane;
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws Exception {
StringBuffer output = new StringBuffer();
output.append("Tesing MySQL Driver installation");
try {
String className = "com.mysql.jdbc.Driver";
Class driverObject = Class.forName(className);
output.append("Driver: "+driverObject+"\n");
output.append("Driver Installation Successful");
JOptionPane.showMessageDialog(null, output);
}
catch(Exception x){
output = new StringBuffer();
output.append("Driver Installation FAILED\n");
JOptionPane.showMessageDialog(null,output);
System.out.println("Failed Driver Error:" +x.getMessage());
}
}
}
* on this part is an example of how to connect the mysql database using java
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.SQLException;
public class JDBCDriverInformation {
static String userid = "root";
static String password = "";
static String url = "jdbc:mysql://localhost:3306/databaseapp";
static Connection con = null;
static String className = "com.mysql.jdbc.Driver";
public static void main(String[] args) throws Exception {
Connection con = getMySQLJDBCConnection();
if (con != null) {
System.out.println("Got Connection");
DatabaseMetaData meta = con.getMetaData();
System.out.println("Driver Name : " + meta.getDriverName());
System.out.println("Driver Version : " + meta.getDriverVersion());
} else {
System.out.println("Could not get connection");
}
}
public static Connection getMySQLJDBCConnection() {
try {
Class.forName(className);
} catch (java.lang.ClassNotFoundException e) {
System.err.print("ClassNotFoundException: ");
System.err.println(e.getMessage());
}
try {
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/databaseapp", userid, password);
} catch (SQLException ex) {
System.err.println("SQLException: " + ex.getMessage());
}
return con;
}
}
Hope you can run successfully in this given example.
Subscribe to:
Posts (Atom)