>
EVO TI OVO: radio 100%
Input_test.java
Code:
/*
* Created on Aug 3, 2006
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
import java.io.*;
/**
* @author ndn5293
* this class is just for testing purposes. It just gets input from the
user.
*/
public class Input_test {
private static BufferedReader stdin = new BufferedReader( new
InputStreamReader( System.in ) );
private ConnectDB db;
public Input_test()
{
try{
getInput();
}
catch(Exception e){e.printStackTrace();
}
}
public static void main(String[] args) {
new Input_test();
}
public void getInput() throws Exception
{
// Prompt the user
System.out.print( "user: " );
// Read a line of text from the user.
String user = stdin.readLine();
//user = user.toUpperCase();
System.out.print( "pass: " );
String pass = stdin.readLine();
//pass = pass.toUpperCase();
System.out.print( "host: " );
String host = stdin.readLine();
//host = host.toUpperCase();
db = new ConnectDB();
db.dbConnect(user,pass,host);
}
}
i ConnectDB.java
Code:
/*
* Created on Aug 3, 2006
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
/**
* @author ndn5293
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class ConnectDB {
Connection conn;
public void dbConnect(String user, String pass, String host)
{
try
{
Class.forName("com.mysql.jdbc.Driver");
String s = "jdbc:mysql://" + host + "/?user=" + user + "&password=" +
pass;
Connection conn = DriverManager.getConnection(s);
}
catch ( ClassNotFoundException cnfex )
{
System.err.println( "Failed to load JDBC/ODBC driver." );
System.exit( 1 );
}
catch (SQLException ex)
{
JOptionPane.showMessageDialog(null,"" + ex.getMessage() +
"nSQLState: " + ex.getSQLState() + "nVendorError: " +
ex.getErrorCode(),"Error",JOptionPane.ERROR_MESSAGE);
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
}
finally
{
if (conn != null)
{
try
{
conn.close ();
System.out.println ("Database connection terminated");
}
catch (Exception e) { /* ignore close errors */ }
}
}
}
public int countIt(int count, String user, String pass, String host)
{
if(count < 30)
{
JOptionPane.showMessageDialog(null,"Connecting..." + count, "OK",
JOptionPane.ERROR_MESSAGE);
dbConnect(user, pass, host);
}
else
{
JOptionPane.showMessageDialog(null,"Too many tries", "Error",
JOptionPane.ERROR_MESSAGE);
}
count++;
return count;
}
}
pozdrav