Here I give you an example of Java code to connect to databases known, the connection to Access is direct, do not need the jdbc odbc bridge. Class
ConexionBD :
import java.sql.Connection;
java.sql.DriverManager import;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/ **
* * @ author ELDI
* / public class
ConexionBD {/ ** * Variable
that defines the type of database to connect, you can take
* values: MySQL, SQLServer, Oracle, Access
* / public static String
tipoBD;
public static Connection connection;
Statement
public static status;
/ **
* Name
database * / public static String
miBaseDatos;
/ ** * User name
the
database * / public static String userName
;
/ ** * Password
the
database * / public static String password
;
/ ** * Method
carrier
the connection * / public static void
connect () {String
nombreDriver = null;
String url = null;
portnumber String = null;
String serverName = null;
if (tipoBD.equals ("MySQL")) {
nombreDriver = "com.mysql.jdbc.Driver";
Servername = "localhost";
portnumber = "3306";
url = "jdbc: mysql: / /" + ServerName + ":" + numeroPuerto + "/" + miBaseDatos;
} else if (tipoBD.equals("SQLServer")) {
nombreDriver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
nombreServidor = "localhost";
numeroPuerto = "1433";
url = "jdbc:sqlserver://" + nombreServidor + ":" + numeroPuerto + "/" + miBaseDatos;
} else if (tipoBD.equals("Oracle")) {
nombreDriver = "oracle.jdbc.driver.OracleDriver";
nombreServidor = "localhost";
numeroPuerto = "1521";
url = "jdbc:oracle:thin:@" + ServerName + ":" + portnumber + ":" + miBaseDatos;
} else if (tipoBD.equals ("Access")) {
nombreDriver = "sun.jdbc.odbc.JdbcOdbcDriver"
Servername = "Driver = {Microsoft Access Driver (*. mdb)}; DBQ = ";
portnumber =" drivered = 22, READONLY = true} ";
url =" jdbc: odbc: "+ serverName + miBaseDatos + portnumber;}
try {Class.forName (nombreDriver). newInstance ();
connection = DriverManager.getConnection (url, userName, password);
conexion.createStatement status = ();
} catch (IllegalAccessException e) {
System.err.println (e.getMessage ());
} catch (InstantiationException e) {
System.err.println (e.getMessage ());} catch
(ClassNotFoundException e) {
System.err.println (e.getMessage ());
} catch (SQLException e) {
System.err.println (e.getMessage ());}
} / * * * Method
manager
close the connection * / public static void
disconnect () {try {
estatuto.close ();
conexion.close ();
} catch (SQLException e) {
System.err.println (e.getMessage ());}
} / ** * Method to execute
queries INSERT, UPDATE, DELETE
* @ param String querySQL
SQL query * / public static void
executeUpdateQuery (String querySQL) {try {
connect ();
estatuto.executeUpdate (querySQL)
disconnect ();
} catch (SQLException e) {
System.err.println (e.getMessage ());
}}
/ ** * Method
to run queries of type SELECT * @ param querySQL
String
SQL query * @ return ResultSet
with the values \u200b\u200bfound * / public static ResultSet executeQuery
(String querySQL) {try {
connect ();
estatuto.executeQuery return (querySQL)
} catch (SQLException e) {
System.err.println (e.getMessage ());
} return null;}
} Main example:
public static void main (String [] args) {
ConexionBD.tipoBD = "Access";
ConexionBD.miBaseDatos = "curso1.mdb";
ConexionBD.nombreUsuario = "jdbc";
ConexionBD.contrasena = "jdbc";
try {
ResultSet resultSet = ConexionBD.executeQuery("select * FROM lista");
while (resultSet.next()) {
String matricula = resultSet.getString("MATRICULA");
String nombre = resultSet.getString("NOMBRE");
int ex1 = resultSet.getInt("EX1");
int ex2 = resultSet.getInt("EX2");
int ex3 = resultSet.getInt("EX3");
System.out.println (tuition + "" + name + "" + ex1 + "" + ex2 + "" + ex3);}
ConexionBD.desconectar ();
} catch (SQLException e) {
System. err.println (e.getMessage ());
}}
0 comments:
Post a Comment