Java Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingJava Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old June 17th, 2004, 06:57 AM
javadev javadev is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 2 javadev User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Unhappy Need help connecting to MySql database with Java

I am having problems finding any infofmation on how to
connect to a MySql database through java.
If anyone can help me I will be very greatfull.

Reply With Quote
  #2  
Old June 17th, 2004, 08:46 AM
zniter zniter is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 1 zniter User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
hi this is how my oracle work... maybe is the same as the mysql....

import javax.swing.*;
import java.sql.*;

public class connect{
static Connection conn = null;
static Statement stmt;
static String url="jdbcdbc:AreaID";
static ResultSet rs,aaaaa;
static String query = "SELECT * FROM PhoneBook";
static Connection m_connection = null;
static Statement stmt1;
static int x = 0;
public connect(){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection(url);
stmt = conn.createStatement();
//orcale connection here
Class.forName ("oracle.jdbc.driver.OracleDriver");
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
m_connection = DriverManager.getConnection("jdbcracle:thin:@j2mew-ser:1521:Fyp", "system", "manager"); stmt1 = m_connection.createStatement();
rs = stmt.executeQuery(query);
//aaaaa = stmt1.executeQuery("DELETE FROM BuildingInfo");
while (rs.next()){
//try
//{
//System.out.println("INSERT INTO MapCorrd VALUES('"+rs.getString("x1")+"','"+rs.getString("y1")+"','"+rs.getString("x2")+"','"+rs.getString("y2")+"','"+rs.getString("MapID")+"','"+rs.getString("sq")+"')");
//stmt1.executeQuery("INSERT INTO BuildingInfo VALUES('"+rs.getString("Categories")+"','"+rs.getString("Landmark")+"','"+rs.getString("BusStop")+"','"+rs.getString("Streetname")+"','"+rs.getString("Repeated")+"')");
//stmt1.executeQuery("INSERT INTO Street VALUES('"+rs.getString("street")+"','"+rs.getString("areaid")+"')");
stmt1.executeQuery("INSERT INTO PhoneBook VALUES('"+rs.getString("PhoneID")+"','"+rs.getString("Name")+"','"+rs.getString("PhoneNo")+"')");
//System.out.println(x++);
//aaaaa = stmt1.executeQuery("INSERT INTO MapCorrd VALUES('"+rs.getString("x1")+"','"+rs.getString("y1")+"','"+rs.getString("x2")+"','"+rs.getString("y2")+"','"+rs.getString("MapID")+"','"+rs.getString("sq")+"')");
//}catch(SQLException cnfs){}
}


}catch(ClassNotFoundException cnfs){System.out.print(cnfs);}
catch(SQLException cnfs){System.out.print(cnfs);}


}
public static void main(String args[])
{
connect Connect = new connect();
}
}

Reply With Quote
  #3  
Old June 17th, 2004, 11:05 AM
Edster99 Edster99 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Location: New Hampshire
Posts: 1 Edster99 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via Yahoo to Edster99
Thumbs up Make sure the database access jar is there and environment variables are set.

Quote:
Originally Posted by javadev
I am having problems finding any infofmation on how to
connect to a MySql database through java.
If anyone can help me I will be very greatfull.

Go to the mysql\doc and pull up the index html. Look under install notes.
1. Start the database server mysqld --console
2. mysql -u root
3. change the passwords to root
4. add a user account
5. run winmysql admin.exe
6. download "mysql connector J" to support JDBC
7. take the jar files and >jar -xvf jta-spec1_0_1.jar to load files
8. make sure those jar files are in a class path (I'm in a sun environment, so I have to use their filemount thingy... you may just be able to add to classpath)
9. set class paths in autoexec.bat
10. write a sql select for the test table and see what you get...


My code looks like this--------------------

import java.math.* ;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.*;
public class select
{
// String defining database connection
static final String dbURI = "jdbc:mysql:///test";
public static void main(String args[])
throws ClassNotFoundException, SQLException {
// Connection reference
Connection conn = null;

try {
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
//System.setProperty("jdbc.drivers","org.gjt.mm.mysql.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost/test?user=prof&password=gunk");
//rds--dbname,rdsindia--username,tryme--password
Statement smt = conn.createStatement() ;
smt.execute("create table test(no varchar(20))") ;
} catch (Exception e ) { System.out.println(e) ;}
System.out.println("db working fine");
}
}
//End of code

Reply With Quote
  #4  
Old June 18th, 2004, 04:38 AM
javadev javadev is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 2 javadev User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks you help me alot.
It ended up been a real simple problem that I kept missing.
thanks again for the help.

Javadev

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJava Development > Need help connecting to MySql database with Java


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT