JDBC——Implemente Database Connection
Interface Database
//source:
package beaver;
import java.sql.Connection;
import java.sql.SQLException;
public interface Database {
/**
* Returns a database connection.
* The database connection that is returned is always the same.
* @return A Database Connection
*/
public abstract Connection getConnection();
/**
* Returns an independent connection.
* Returns a new database connection for each call
* @return A Database Connection
*/
public abstract Connection getNewConnection() throws SQLException;
public abstract String getDatabaseName();
public abstract String getDriverName();
public abstract String getPassword();
public abstract String getUserName();
}
GenericDatabase .java
//source
package beaver;
import java.sql.*;
public class GenericDatabase implements Database {
private String driverName;
private String databaseName;
private String userName;
private String password;
private Connection con;
/**
* @throws ClassNotFoundException
* @throws SQLException
*/
public GenericDatabase(
String _driverName,
String _databaseName,
String _userName,
String _password)
throws ClassNotFoundException, SQLException {
driverName = _driverName;
databaseName = _databaseName;
userName = _userName;
password = _password;
try{
Class.forName(driverName);
con = DriverManager.getConnection(
databaseName,
userName,
password);
}catch(Exception e){
System.out.println("fail");
System.out.println(e.getMessage());
}
}
public String getDatabaseName() {
return databaseName;
}
public String getDriverName() {
return driverName;
}
public String getPassword() {
return password;
}
public String getUserName() {
return userName;
}
/**
* Returns a database connection.
* The database connection that is returned is always the same.
* @return A Database Connection
*/
public Connection getConnection() {
return con;
}
/**
* Returns an independent connection.
* Returns a new database connection for each call
* This method retries until it actually gets a connection
* @return A Database Connection
*/
public Connection getNewConnection() {
Connection ncon;
while (true) {
try {
ncon =
DriverManager.getConnection(getDatabaseName(),
getUserName(),
getPassword());
return ncon;
} catch (SQLException e) {
System.err.println(e.getErrorCode() + " " + e.getMessage());
try {
// Wait for half a sec, then try again
Thread.sleep(500);
} catch (InterruptedException ie) {
// ignore
}
}
}
}
}
myDatabase .java
Connect database.If connect local database,ip is "localhost" or "127.0.0.1"
"first" is a database;"root" is usename;"11111" is password.
//source:
package beaver;
import java.sql.*;
public class myDatabase extends GenericDatabase {
public myDatabase ()
throws ClassNotFoundException, SQLException {
super("com.mysql.jdbc.Driver",
"jdbc:mysql://ip:3306/first",
"root",
"11111");
}
}
2009年3月29日星期日
JavaSyntax
Recently I use some JavaSyntax,as follow:
1)Formatter
import java.util.Formatter;
Formatter f=new Formatter(System.out);
//output a string s,give it 40 char size and flush left
f.format("%-40s",s);
//output a double d,give it 40 char size and save 4 decimal and default flush right
f.format("%-40.4f", d);
2)create a .arff file and output data to file
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
File f = new File("d:/" + "weather" + ".arff");
DataOutputStream out=
new DataOutputStream(
new FileOutputStream(f));
out.writeBytes("@relation weather");
//remove blanks among the string
String s="hello world";
out.writeBytes(s.replaceAll(" ", ""));
3)save 4 decimal
import java.text.DecimalFormat;
DecimalFormat digits=new DecimalFormat("0.0000");
double value=d;
out.writeBytes(digits.format(value));
1)Formatter
import java.util.Formatter;
Formatter f=new Formatter(System.out);
//output a string s,give it 40 char size and flush left
f.format("%-40s",s);
//output a double d,give it 40 char size and save 4 decimal and default flush right
f.format("%-40.4f", d);
2)create a .arff file and output data to file
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
File f = new File("d:/" + "weather" + ".arff");
DataOutputStream out=
new DataOutputStream(
new FileOutputStream(f));
out.writeBytes("@relation weather");
//remove blanks among the string
String s="hello world";
out.writeBytes(s.replaceAll(" ", ""));
3)save 4 decimal
import java.text.DecimalFormat;
DecimalFormat digits=new DecimalFormat("0.0000");
double value=d;
out.writeBytes(digits.format(value));
2009年3月14日星期六
first
Some days ago,I see sujitpal's blog,I really like it and admin him.His articles are uesful to me.Therefore I want to create a blog to write myself.I am a postgraduate now,After 2 years,I will graduate from school.All the time I have a faith--Be myself,by myself,realize my dreams.I deeply understand if I want to success I must do my best and by myself,because only can I help myself.I want to be a programmer but I am poor in programming.So I learn some necessary knowledge and technology,but sometimes I feel helpless,even lost direction,but nobody can talk with.Maybe I have little knowledge and need learn more fast.I think I must pay more and more if I want to realize my dreams.The most important is persistence.From now on,I will write something in my blog every day,I believe I can become a useful person.Frighting!
订阅:
评论 (Atom)
