forked from rlawrenc/cosc_304
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestJDBCMySQL_Java6.java
48 lines (45 loc) · 1.05 KB
/
TestJDBCMySQL_Java6.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import java.sql.*;
public class TestJDBCMySQL_Java6
{ public static void main(String[] args)
{ String url = "jdbc:mysql://cosc304.ok.ubc.ca/workson";
String uid = "rlawrenc";
String pw = "todo";
// Note: Loading a driver class is not required.
try
{ // Load driver class
Class.forName("com.mysql.jdbc.Driver");
}
catch (java.lang.ClassNotFoundException e) {
System.err.println("ClassNotFoundException: " +e);
System.exit(1);
}
Connection con = null;
try {
con = DriverManager.getConnection(url, uid, pw);
Statement stmt = con.createStatement();
ResultSet rst = stmt.executeQuery("SELECT ename,salary FROM emp");
System.out.println("Employee Name,Salary");
while (rst.next())
{ System.out.println(rst.getString("ename")+","+rst.getDouble("salary"));
}
}
catch (SQLException ex)
{
System.err.println("SQLException: " + ex);
}
finally
{
if (con != null)
{
try
{
con.close();
}
catch (SQLException ex)
{
System.err.println("SQLException: " + ex);
}
}
}
}
}