实现的功能:window下连接linux的mysql数据库
准备的环境:
windows:1.已经编写好的连接数据库的java测试程序:
package com.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* ResultSet结果集的基本用法(executeQuery()将会返回ResultSet结果集)
* @author zanzan
*
*/
public class Demo2 {
public static void main(String[] args){
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
long start = System.currentTimeMillis();
conn = DriverManager.getConnection("jdbc:mysql://与linux的ip映射的域名:映射的端口号/数据库名称", "mysql数据库登陆名", "密码");
System.out.println(conn);
//进行数据库的查询
String sql = "select id,name from test where id>?";//?表示占位符
//建立PraparedStatemen对象进行SQL静态语句的操作,同时返回PraparedStatememnt对象,导入PraparedStatement包
ps = conn.prepareStatement(sql);
ps.setObject(1, 0);
rs = ps.executeQuery();
//将结果集进行遍历
while(rs.next()){
System.out.println(rs.getInt(1)+"---"+rs.getString(2));
}
long end = System.currentTimeMillis();
System.out.println("建立与数据库的连接所需要的时间"+(end-start)+"ms");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
//关闭顺序是按照先开后关的原则
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(ps!=null){
try {
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(conn!