Thomas Darimont
Erfahrenes Mitglied
Hallo!
Hier findet ihr nochmal alle Beispiele zu den Videoworkshop:
MySQL:
Oracle:
MS SQL Server mit JTDS Treiber:
MS Access:
MS Excel ( Zuvor in Excelsheet alle Zeilen (inklusive Überschrift) auswählen und markierten Bereich benennen) ->
Gruß Tom
Hier findet ihr nochmal alle Beispiele zu den Videoworkshop:
MySQL:
Code:
/**
*
*/
package de.tutorials.workshop.jdbc;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
/**
* @author Tom
*
*
* SET AUTOCOMMIT = 0; create table user(id int, name varchar(32), password
* varchar(32)); insert into user values (0,'thomas', 'foo'); insert into user
* values (1,'juergen', 'bar'); commit;
*
* select * from user;
*
*
*/
public class MySQLJDBCExample {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
// Datasource Konfigurieren
MysqlDataSource mds = new MysqlDataSource();
mds.setUser("root");
mds.setPassword("");
mds.setPortNumber(3306);
mds.setServerName("localhost");
mds.setDatabaseName("test");
Connection con = null;
// create table user(id int not null, name varchar(32), password
// varchar(32), primary key(id));
// select * from user;
try {
con = mds.getConnection();
// // Autocommit deaktivieren
// con.setAutoCommit(false);
Statement stmt = con
.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
// Daten auslesen:
// *
ResultSet rs = stmt.executeQuery("SELECT * FROM user");
// Datenbankspezifische Funktionen aufrufen
// ResultSet rs = stmt
// .executeQuery("SELECT {fn concat(\"Hallo, \",\"Welt!\")}");
// Über Abfrageergebnis iterieren
ResultSetMetaData rsmd = rs.getMetaData();
int clmCnt = rsmd.getColumnCount();
while (rs.next()) {
for (int i = 1; i <= clmCnt; i++) {
System.out.print(rs.getString(i));
System.out.print(" ");
}
System.out.println();
}
// */
// PreparedStatements
// PreparedStatement ps = con.prepareStatement("INSERT INTO user
// VALUES(?,?,?)");
// ps.setInt(1,4711);
// ps.setString(2,"foo");
// ps.setString(3,"bar");
//
// ps.execute();
// Daten einfügen:
// stmt.executeUpdate("INSERT INTO user VALUES(111,'A','B')");
rs.moveToInsertRow();
rs.updateInt(1, 14);
rs.updateString(2, "Tom");
rs.updateString(3, "jdar");
rs.insertRow();
// con.commit();
// Daten aktualisieren:
// stmt.executeUpdate("UPDATE user SET name='c' password='d') WHERE
// id = 111;
// rs.absolute(-1);
// rs.previous();
// rs.updateString(2,"Martin3");
// rs.updateString(3,"lightbox3");
// rs.updateRow();
// Zweite Zeile löschen
// stmt.execute("DELETE FROM user WHERE id = 111");
// rs.beforeFirst();
// rs.absolute(2);
// rs.deleteRow();
stmt.close();
rs.close();
} finally {
if (con != null) {
con.close();
}
}
}
}
Oracle:
Code:
/**
*
*/
package de.tutorials.workshop.jdbc;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import oracle.jdbc.pool.OracleDataSource;
/**
* @author Tom
*
*/
public class OracleJDBCExample {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
// Treiber laden:
Class.forName("oracle.jdbc.OracleDriver");
OracleDataSource ods = new OracleDataSource();
ods.setUser("scott");
ods.setPassword("foobar");
ods.setPortNumber(1521);
ods.setDriverType("thin");
ods.setServerName("localhost");
ods.setDatabaseName("orcl");
Connection con = null;
try {
con = ods.getConnection();
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM emp");
ResultSetMetaData rsmd = rs.getMetaData();
int clmCnt = rsmd.getColumnCount();
while (rs.next()) {
for (int i = 1; i <= clmCnt; i++) {
System.out.print(rs.getString(i));
System.out.print(" ");
}
System.out.println();
}
} finally {
if (con != null) {
con.close();
}
}
}
}
MS SQL Server mit JTDS Treiber:
Code:
/**
*
*/
package de.tutorials.workshop.jdbc;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import net.sourceforge.jtds.jdbcx.JtdsDataSource;
/**
* @author Tom
*
*/
public class MSSQLServerJDBCExample {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
Class.forName("net.sourceforge.jtds.jdbc.Driver");
JtdsDataSource jtds = new JtdsDataSource();
jtds.setUser("foo");
jtds.setPassword("bar");
jtds.setPortNumber(1433);
jtds.setServerName("localhost");
jtds.setDatabaseName("Northwind");
Connection con = null;
try {
con = jtds.getConnection();
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM emp");
ResultSetMetaData rsmd = rs.getMetaData();
int clmCnt = rsmd.getColumnCount();
while (rs.next()) {
for (int i = 1; i <= clmCnt; i++) {
System.out.print(rs.getString(i));
System.out.print(" ");
}
System.out.println("");
}
} finally {
if (con != null) {
con.close();
}
}
}
}
MS Access:
Code:
/**
*
*/
package de.tutorials.workshop.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
/**
* @author Tom
*
*/
public class AccessJdbcOdbcExample {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String dbpath = "E:/stuff/Nordwind.mdb";
Connection con = DriverManager
.getConnection("jdbc:odbc:DRIVER={Microsoft Access Driver (*.mdb)};DBQ="
+ dbpath);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM kunden");
ResultSetMetaData rsmd = rs.getMetaData();
int clmCnt = rsmd.getColumnCount();
while (rs.next()) {
for (int i = 1; i <= clmCnt; i++) {
System.out.print(rs.getString(i));
System.out.print(" ");
}
System.out.println();
}
con.close();
}
}
MS Excel ( Zuvor in Excelsheet alle Zeilen (inklusive Überschrift) auswählen und markierten Bereich benennen) ->
Code:
/**
*
*/
package de.tutorials.workshop.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
/**
* @author Tom
*
*/
public class ExcelJdbcOdbcExample {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String dbpath = "E:/stuff/kunden.xls";
Connection con = DriverManager
.getConnection("jdbc:odbc:DRIVER={Microsoft Excel Driver (*.xls)};DBQ="
+ dbpath);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM artikel");
ResultSetMetaData rsmd = rs.getMetaData();
int clmCnt = rsmd.getColumnCount();
while (rs.next()) {
for (int i = 1; i <= clmCnt; i++) {
System.out.print(rs.getString(i));
System.out.print(" ");
}
System.out.println();
}
con.close();
}
}
Gruß Tom