Java MySql connection (povezava)
Copy mysql-connector-java-3114-bin to C:\Program Files\Java\jre1.6.X\lib\ext
JAVA:
-
import java.sql.Connection;
-
import java.sql.DriverManager;
-
import java.sql.SQLException;
-
-
// Notice, do not import com.mysql.jdbc.*
-
// or you will have problems!
-
-
public class LoadDriver {
-
-
Connection conn = null;
-
-
try {
-
// The newInstance() call is a work around for some
-
// broken Java implementations
-
-
Class.forName("com.mysql.jdbc.Driver").newInstance();
-
conn =
-
"user=monty&password=greatsqldb");
-
-
Statement stmt = null;
-
ResultSet rs = null;
-
-
stmt = conn.createStatement();
-
rs = stmt.executeQuery("SELECT foo FROM bar");
-
-
// or alternatively, if you don't know ahead of time that
-
// the query will be a SELECT...
-
-
if (stmt.execute("SELECT foo FROM bar")) {
-
rs = stmt.getResultSet();
-
}
-
-
// handle any errors
-
-
}
-
finally {
-
// it is a good idea to release
-
// resources in a finally{} block
-
// in reverse-order of their creation
-
// if they are no-longer needed
-
-
if (rs != null) {
-
try {
-
rs.close();
-
-
rs = null;
-
}
-
-
if (stmt != null) {
-
try {
-
stmt.close();
-
-
stmt = null;
-
}
-
}
-
}
JAVA:
-
import java.sql.*;
-
-
public class Jdbc10 {
-
"Copyright 2004, R.G.Baldwin");
-
try {
-
Statement stmt;
-
ResultSet rs;
-
-
//Register the JDBC driver for MySQL.
-
Class.forName("com.mysql.jdbc.Driver");
-
-
//Define URL of database server for
-
// database named JunkDB on the localhost
-
// with the default port number 3306.
-
String url =
-
"jdbc:mysql://localhost:3306/JunkDB";
-
-
//Get a connection to the database for a
-
// user named auser with the password
-
// drowssap, which is password spelled
-
// backwards.
-
Connection con =
-
url,"auser", "drowssap");
-
-
//Display URL and connection information
-
-
//Get a Statement object
-
stmt = con.createStatement();
-
-
//As a precaution, delete myTable if it
-
// already exists as residue from a
-
// previous run. Otherwise, if the table
-
// already exists and an attempt is made
-
// to create it, an exception will be
-
// thrown.
-
try{
-
stmt.executeUpdate("DROP TABLE myTable");
-
"No existing table to delete");
-
}//end catch
-
-
//Create a table in the database named
-
// myTable.
-
stmt.executeUpdate(
-
"CREATE TABLE myTable(test_id int," +
-
"test_val char(15) not null)");
-
-
//Insert some values into the table
-
stmt.executeUpdate(
-
"INSERT INTO myTable(test_id, " +
-
"test_val) VALUES(1,'One')");
-
-
//Get another statement object initialized
-
// as shown.
-
stmt = con.createStatement(
-
ResultSet.TYPE_SCROLL_INSENSITIVE,
-
-
//Query the database, storing the result
-
// in an object of type ResultSet
-
rs = stmt.executeQuery("SELECT * " +
-
"from myTable ORDER BY test_id");
-
-
//Use the methods of class ResultSet in a
-
// loop to display all of the data in the
-
// database.
-
while(rs.next()){
-
int theInt= rs.getInt("test_id");
-
+ "\tstr = " + str);
-
}//end while loop
-
-
//Display the data in a specific row using
-
// the rs.absolute method.
-
"Display row number 2:");
-
if( rs.absolute(2) ){
-
int theInt= rs.getInt("test_id");
-
+ "\tstr = " + str);
-
}//end if
-
-
//Delete the table and close the connection
-
// to the database
-
stmt.executeUpdate("DROP TABLE myTable");
-
con.close();
-
e.printStackTrace();
-
}//end catch
-
}//end main
-
}//end class Jdbc10
Categories: Java, Malo mešano