工程导入mysql-connector-java-5.1.7-bin.jar后;
public static void main(String[] args)
{
// 驱动程序名
String driver = "com.mysql.jdbc.Driver";
// URL指向要访问的数据库名scutcs
String url = "jdbc:mysql://127.0.0.1:3305/test";
// MySQL配置时的用户名
String user = "root";
// MySQL配置时的密码
String password = "123456";
try
{
// 加载驱动程序
Class.forName(driver);
// 连续数据库
Connection conn = DriverManager.getConnection(url, user, password);
if(!conn.isClosed())
{
System.out.println("Succeeded connecting to the Database!");
}
// statement用来执行SQL语句
Statement statement = conn.createStatement();
// 要执行的SQL语句
String sql = "select * from test1";
String name = null;
// 结果集
ResultSet rs = statement.executeQuery(sql);
while(rs.next())
{
// 选择sname这列数据
name = rs.getString("title");
// 输出结果
System.out.println(rs.getString("id") + " " + name);
}
rs.close();
conn.close();
}
catch(ClassNotFoundException e)
{
System.out.println("Sorry,can`t find the Driver!");
e.printStackTrace();
}
catch(SQLException e)
{
e.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}
}