|
hi, I am having problems with MySQL database and java. I have one interface called DBConnection and one class called MySQLConnection. The third class, MyStatements i'm having problems with. I am trying to put the main method inside MyStatements but nothing happens in my database even though I seemes to connect to the database. So any help really appreciatet.
public interface DBConnection {
public void setConHost(String pHost);
public void setUser(String pUser);
public void setPass(String pPass);
public void connect();
public void disconnect();
public Connection getConnection() throws SQLException;
}
public class MySQLConnection implements DBConnection {
private String mConString;
private String mUser;
private String mPass;
private Connection mCon;
private boolean isConnected;
public MySQLConnection(){
try{
DriverManager.registerDriver (new com.mysql.jdbc.Driver());
}
catch(SQLException e){
}
isConnected = false;
}
public MySQLConnection(String pHost, String pUser, String pPass){
this();
setConHost(pHost);
setUser(pUser);
setPass(pPass);
}
public void setConHost(String pHost){
mConString = "jdbc:mysql:"+pHost;
}
public void setUser(String pUser){
mUser = pUser;
}
public void setPass(String pPass){
mPass = pPass;
}
public void connect(){
try{
System.out.println(mConString);
mCon = DriverManager.getConnection(mConString,mUser,mPass);
isConnected=true;
}
catch(SQLException e){
());
}
}
public void disconnect(){
try{
mCon.close();
isConnected=false;
}
catch(SQLException e){
());
}
}
public Connection getConnection() throws SQLException{
if(isConnected == false){
SQLException e = new SQLException("error");
throw e;
}
return mCon;
}
}
And finally, here is the class where I'm having problem with:
class MyStatements
{
Connection con = null;
public MyStatements(Connection con)
{
this.con = con;
}
public static void main(String[] args)
{
Connection con=null;
DBConnection db_con = new MySQLConnection("//localhost/java","root","pass");
db_con.connect();
try{
con= db_con.getConnection();
System.out.println("connection successful");
}
catch(SQLException e){
System.err.println("DBTest:\t"+e.getMessage());
System.exit(1);
}
}
public void makeCoffeTable()
{
try
{
String createString = "create table COFFEES " +
"(COF_NAME VARCHAR(32), " +
"SUP_ID INTEGER, " +
"PRICE FLOAT, " +
"SALES INTEGER, " +
"TOTAL INTEGER)";
Statement stmt = con.createStatement();
stmt.executeUpdate(createString);
}
catch (SQLException sql)
{
System.err.println("Error executing statements");
System.out.println("error: " +sql.getMessage());
}
}
public void makeSupTable()
{
try
{
String createString = "create table SUPPLIERS " +
"(SUP_ID INTEGER, " +
"SUP_NAME VARCHAR(32), " +
"STREET TEXT, " +
"CITY TEXT, " +
"STATE TEXT, "+
"ZIP INTEGER)";
Statement stmt = con.createStatement();
stmt.executeUpdate(createString);
}
catch (SQLException sql)
{
System.err.println("Error executing statements");
System.out.println("error: " +sql.getMessage());
}
}
}
|
|
|
hi
just use this main method
one more thing "//loccalhost/java",
here "java" is ur database name.
public static void main(String[] args)
{
Connection con=null;
DBConnection db_con = new MySQLConnection("//localhost/java","root","");
db_con.connect();
try{
con= db_con.getConnection();
System.out.println("connection successful");
MyStatements ms=new MyStatements(con);
ms.makeCoffeTable();
ms.makeSupTable();
}
catch(SQLException e){
System.err.println("DBTest:\t"+e.getMessage());
System.exit(1);
}
}
i checked , its workig fine, i added just three lines
|
|
|
|
|
|
|
|