codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  Databaser in java  gosi at 19:32 on Monday, May 15, 2006
 

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());
}
}
}


  Re: Databaser in java  miteshbhimjiyaani at 11:33 on Monday, May 22, 2006
 

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








CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums








Recent Forum Threads
•  Re: Help: Trouble with z-Index and SELECT lists
•  Dynamic crystal report formatting
•  URL rewriting doesnt seem to work with xml sitemap provider and menus
•  This code is not working
•  Re: doubt in asp.net
•  Scraping, Pulling, Calculating, and Displaying
•  Re: How to open a MS Word document from Javascript
•  session time out
•  Scroll To Not Fully working in IE...


Recent Articles
Decode and Encode UTF-8
Javascript Get Selected Text
Javascript OnMouseDown
Pointer types and Arrays
What is a pointer in C?
Multiple submit buttons with form validation
Understanding Hibernate ORM for Java/J2EE
HTTP screen-scraping and caching
a javascript calculator
A simple way to JTable


© Copyright codetoad.com 2001-2006