1 package demo;
2
3 import java.sql.*;
4 /**
5 *最原始的jdbc连接方式
6 *@author sy
7 */
8 public class MysqlConnection
9 {
10 public static void main(String[] args)
11 {
12 try
13 {
14 String url="jdbc:mysql://localhost/viscms";
15 String user="root";
16 String pwd="root";
17
18 //加载驱动,这一句也可写为:Class.forName("com.mysql.jdbc.Driver");
19 Class.forName("com.mysql.jdbc.Driver").newInstance();
20 //建立到MySQL的连接
21 Connection conn = DriverManager.getConnection(url,user, pwd);
22
23 //执行SQL语句
24 Statement stmt = conn.createStatement();//创建语句对象,用以执行sql语言
25 ResultSet rs = stmt.executeQuery("select username from jc_user");
26
27 //处理结果集
28 while (rs.next())
29 {
30 String name = rs.getString("username");
31 System.out.println(name);
32 }
33 rs.close();//关闭数据库
34 conn.close();
35 }
36 catch (Exception ex)
37 {
38 System.out.println("Error : " + ex.toString());
39 }
40 }
41 }