473,508 Members | 2,374 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

FYI: Connect and Select from Java on Linux

I found the following interesting and wanted to pass it along
Reference (TIP 12):
http://prdownloads.sourceforge.net/s...l.txt?download

Connect and Select from Java

//
// mysql-connector-java-3.0.11-stable-bin.jar or later must be
downloaded
// and installed from:
// http://dev.mysql.com/downloads/connector/j/3.0.html
//
// Edit to include your username and password:
// String userName = "yourusername";
// String password = "passord";
//
//
// Compile:
// javac Select.java
// (or you can also use GCC's gcj "gcj -C Select.java")
// REF: ttp://gcc.gnu.org/java/papers/cni/t1.html
// Run:
// java Select
//
//
// The program above assumes "exams" has been created in
// the "test" database. If you create "exams" change the
// select statment to read:
// SELECT pkey,name,exam,score FROM yourdatabase.exams
//
//
// CREATE TABLE exams (
// pkey int(11) NOT NULL auto_increment,
// name varchar(15),
// exam int,
// score int,
// PRIMARY KEY (pkey)
//
// );
//
// insert into exams (name,exam,score) values ('Bob',1,75);
// insert into exams (name,exam,score) values ('Bob',2,77);
// insert into exams (name,exam,score) values ('Bob',3,78);

// insert into exams (name,exam,score) values ('Bob',4,80);
//
// insert into exams (name,exam,score) values ('Sue',1,90);
// insert into exams (name,exam,score) values ('Sue',2,97);
// insert into exams (name,exam,score) values ('Sue',3,98);
// insert into exams (name,exam,score) values ('Sue',4,99);
//

import java.sql.*;

public class Select
{

public static void main (String[ ] args)
{
Connection conn = null;
String url = "jdbc:mysql://localhost/";
String userName = "username1";
String password = "password1";

try
{
Class.forName
("com.mysql.jdbc.Driver").newInstance ( );
conn = DriverManager.getConnection (url,
userName, password);
// System.out.println ("Connected");
Statement s = conn.createStatement ( );
s.executeQuery ("SELECT
pkey,name,exam,score FROM test.exams");
ResultSet rs = s.getResultSet ( );
int count = 0;
while (rs.next ( )) // loop through rows
of result set
{
int pkey = rs.getInt (1);
String name = rs.getString(2);
int exam = rs.getInt(3);
int score = rs.getInt(4);
++count;

System.out.println (count +
",inum: " + pkey + ",name: " +
name + ",exam: " + exam +
",score: " + score );

}
rs.close ( ); // close result set
s.close ( ); // close statement
System.out.println (count + " rows were
returned");

}
catch (Exception e)
{
System.err.println ("Cannot connect to
server");
}
finally
{
if (conn != null)
{
try
{
conn.close ( );
// System.out.println
("Disconnected"); /* for debugging */
}
catch (Exception e) { /* ignore close
errors */ }
}
}
}

}

//end program.

Anyway, I thought I'd pass it along.

Regards,

Mike Chirico
Jul 20 '05 #1
0 1677

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
27874
by: MLH | last post by:
Using MS Access, I have attached to MySQL servers in other states and other countries on the other side of my router. But when I use the MySQL ODBC driver 3.51 to connect to a MySQL server on my...
30
7380
by: aka | last post by:
Hi I have a DB2 v8.1 on AIX and DB2 Connect EE on Solaris wich is connected to OS/390 DB2 subsystems via APPC / SNA. I have cataloged the DB2 Connect instance as tcpip node and then the Host DB...
5
5662
by: mayamorning123 | last post by:
A comparison among six VSS remote tools including SourceOffSite , SourceAnyWhere, VSS Connect, SourceXT, VSS Remoting, VSS.NET To view the full article, please visit...
5
3543
by: mjf | last post by:
I'd like to connect from any Linux machine to the DB2 on a Linux server via an IP adddress (or any available method that allows us to connect to DB2 from another machine). I know we can do it in...
2
3317
by: vikas.bhatia | last post by:
Is this still true? http://groups.google.com/group/comp.databases.ibm-db2/browse_thread/thread/58c6c36ce9006d50/778f98749d8e2983?lnk=st&q=DB2+Connect+product+license&rnum=6#778f98749d8e2983 would...
14
11622
by: Khan | last post by:
Hello, I want to connect my intranet server (with php/apache) to the other informix database server, But I didnt it. I dont know how i can do it? Can i use php_informix extensions for example...
30
1997
by: HangEveryRepubliKKKan | last post by:
Ahahahaha.. Lets see if I get this Demented Lintard reasoning right. Vista is a failure becuase after 300 days after it's release, it only has 7.5% of the OS market while the Lintard OS, is a...
10
3378
rhitam30111985
by: rhitam30111985 | last post by:
hi all .. java/jdbc newbie here ... i cant seem to perform the most basic step for database connectivity using java . here is my code : import java.sql.*; public class connect { ...
0
7120
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7323
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7039
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5626
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5050
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4706
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3192
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1553
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
763
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.