Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Java und MySQL-JDBC Datenbankzugriff

Empfohlene Antworten

Hallo JavaProgrammierer,

wie steuert man eine MySQL-Datenbank unter Linux an ???

ist das schon der Code oder muss ich noch was ändern - bin neu in der Materie ???

ODBC - ist doch Windows oder ???

Connection connection =

DriverManager.getConnection

("jdbc:odbc:test", "Name", "Passwort");

danke für Hilfe

paule

Hallo,

unter Linux nimmt man nicht odbc.

Es ist zwar möglich, aber auf keinen Fall zu empfehlen.

Du brauchst einen JDBC Treiber für MySQL.

z.B. den hier http://mmmysql.sourceforge.net/

Wie du dich dann zur Datenbank verbindest, steht in der Doku des Treibers, mit ausführlichen Beispielen.

Gruß Jaraz

jo,

dank dir

  • 2 Wochen später...

Also gut, du hast es so gewollt.

Das folgende Programm ist ein Test von mir, wie man am schnellsten Daten in Mysql über Java mit einer eindeutigen ID einfügt.

Als erstes reicht es, wenn du dir Methode 0 anschaust und nachbaust.

Gruß Jaraz

-------------------------------------------------------------

import java.sql.*;
import java.text.*;
import java.net.*;

class MysqlTest {

private Connection con;
private ResultSet rs;
private int nr;
private PreparedStatement psUpdate;
private PreparedStatement psInsert1;
private PreparedStatement psInsert2;
private PreparedStatement psSelect;
private Statement stmt;
private long t0;
private long t1;
private double deltaT0;
private double deltaT1;
private double deltaT2;
private static int loops;
private static String host;
private static String user;
private static String pass;
private static String db;
private static boolean index;

public MysqlTest() throws Exception {

//Start von Methode 0, einfach einfügen

Class.forName("org.gjt.mm.mysql.Driver");
con = DriverManager.getConnection("jdbc:mysql://"+host+"/"+db+"", user, pass);
psInsert1 = con.prepareStatement("insert into test1 set myid = ?, nr = ?;");
psInsert2 = con.prepareStatement("insert into test2 set myid = ?, nr = ?;");
stmt = con.createStatement();

stmt.executeQuery("create table test1(myid INT UNSIGNED NOT NULL PRIMARY KEY, nr INT UNSIGNED NOT NULL)");
stmt.executeQuery("create table test2(myid INT UNSIGNED NOT NULL PRIMARY KEY, nr INT UNSIGNED NOT NULL)");

t0 = System.currentTimeMillis();
for(int i=1; i<=loops; i++){
psInsert1.setInt(1,i);
psInsert1.setInt(2,i);
psInsert1.executeQuery();
psInsert2.setInt(1,i);
psInsert2.setInt(2,i);
psInsert2.executeQuery();
}
t1 = System.currentTimeMillis();
deltaT0 = t1-t0;
System.out.println("\nMethode 0 einfach einfügen.");
System.out.println(loops+" Datensaetze brauchen "+DecimalFormat.getInstance().format(deltaT0/1000.)+" Sekunden.");
stmt.executeQuery("drop table test1");
stmt.executeQuery("drop table test2");
con.close();

//Ende von Methode 0
//----------------------------------------------------------------
//Start von Methode 1 einfügen mit einer Sequenztabelle und der Funktion LAST_INSERT_ID

con = DriverManager.getConnection("jdbc:mysql://"+host+"/"+db+"", user, pass);
psUpdate = con.prepareStatement("update seq_table set seq = LAST_INSERT_ID(seq+1);");
psInsert1 = con.prepareStatement("insert into test1 set myid = last_insert_id(), nr = ?;");
psInsert2 = con.prepareStatement("insert into test2 set myid = last_insert_id(), nr = ?;");
stmt = con.createStatement();

stmt.executeQuery("create table seq_table(seq INT UNSIGNED NOT NULL)");
stmt.executeQuery("INSERT INTO seq_table VALUES(0)");
if(index){
stmt.executeQuery("create table test1(myid INT UNSIGNED NOT NULL PRIMARY KEY, nr INT UNSIGNED NOT NULL, index(nr))");
stmt.executeQuery("create table test2(myid INT UNSIGNED NOT NULL PRIMARY KEY, nr INT UNSIGNED NOT NULL, index(nr))");
}else{
stmt.executeQuery("create table test1(myid INT UNSIGNED NOT NULL PRIMARY KEY, nr INT UNSIGNED NOT NULL)");
stmt.executeQuery("create table test2(myid INT UNSIGNED NOT NULL PRIMARY KEY, nr INT UNSIGNED NOT NULL)");
}
t0 = System.currentTimeMillis();
for(int i=1; i<=loops; i++){
psUpdate.executeQuery();
psInsert1.setInt(1,i);
psInsert1.executeQuery();
psInsert2.setInt(1,i);
psInsert2.executeQuery();
}
t1 = System.currentTimeMillis();
deltaT1 = t1-t0;
System.out.println("\nMethode 1 mit einer Sequenz und der Funktion LAST_INSERT_ID.");
System.out.println(loops+" Datensaetze brauchen "+DecimalFormat.getInstance().format(deltaT1/1000.)+" Sekunden.");
stmt.executeQuery("drop table seq_table");
stmt.executeQuery("drop table test1");
stmt.executeQuery("drop table test2");
con.close();

//Ende von Methode 1
//----------------------------------------------------------------
//Start von Methode 2 einfügen ueber ein SELECT nach dem ersten INSERT

con = DriverManager.getConnection("jdbc:mysql://"+host+"/"+db+"", user, pass);
psInsert1 = con.prepareStatement("insert into test1 set nr = ?;");
psSelect = con.prepareStatement("select myid from test1 where nr = ?;");
psInsert2 = con.prepareStatement("insert into test2 set myid = ?, nr = ?;");
stmt = con.createStatement();
if(index){
stmt.executeQuery("create table test1(myid INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT, nr INT UNSIGNED NOT NULL, index(nr))");
stmt.executeQuery("create table test2(myid INT UNSIGNED NOT NULL PRIMARY KEY, nr INT UNSIGNED NOT NULL, index(nr))");
}else{
stmt.executeQuery("create table test1(myid INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT, nr INT UNSIGNED NOT NULL)");
stmt.executeQuery("create table test2(myid INT UNSIGNED NOT NULL PRIMARY KEY, nr INT UNSIGNED NOT NULL)");
}
t0 = System.currentTimeMillis();
for(int i=1; i<=loops; i++){
psInsert1.setInt(1,i);
psInsert1.executeQuery();
psSelect.setInt(1,i);
rs = psSelect.executeQuery();
rs.next();
nr = rs.getInt(1);
psInsert2.setInt(1,nr);
psInsert2.setInt(2,i);
psInsert2.executeQuery();
}
t1 = System.currentTimeMillis();
deltaT2 = t1-t0;
System.out.println("\nMethode 2 ueber ein SELECT nach dem ersten INSERT");
System.out.println(loops+" Datensaetze brauchen "+DecimalFormat.getInstance().format(deltaT2/1000.)+" Sekunden.");
System.out.println("\nDie 1.te Methode benoetigt "+DecimalFormat.getInstance().format(-100+((deltaT1/1000)/((deltaT0/1000)/100)))+"% mehr Zeit als die 0.te Methode");
System.out.println("Die 2.te Methode benoetigt "+DecimalFormat.getInstance().format(-100+((deltaT2/1000)/((deltaT1/1000)/100)))+"% mehr Zeit als die 1.te Methode");
System.out.println("Die 2.te Methode benoetigt "+DecimalFormat.getInstance().format(-100+((deltaT2/1000)/((deltaT0/1000)/100)))+"% mehr Zeit als die 0.te Methode\n\n");
stmt.executeQuery("drop table test1");
stmt.executeQuery("drop table test2");
con.close();
}

//Ende von Methode 2

public static void main(String args[]) throws Exception {
host = "host";
db = "db";
user = "user";
pass = "pass";
index = true;
loops = 100000;
MysqlTest mysqlTest = new MysqlTest();
}
}
[/PHP]

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.