package com.vips.stone.demo;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class jdbcDemo {
java.sql.Connection conn =null;
PreparedStatement stmt = null;
private String url = "jdbc:mysql://10.199.145.228:3306/vip_mo";
private String username = "vipshop";
private String password = "dWwkv%+!1cQf$FNy";
private String sql = "select * from t_mo_top_menu where id =?";
/**
*
* @param param_value
* 需要查询的id=param_value
* @throws SQLException
*/
public void JDBCTest(Object param_value) throws SQLException {
try {
//STEP 1: 注册JDBC驱动程序
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println("找不到驱动程序类 ,加载驱动失败!");
e.printStackTrace();
}
try {
//STEP 2: 建立数据库连接
conn = DriverManager.getConnection(url, username, password);
}catch (SQLException e){
System.out.println("数据库连接失败!");
e.printStackTrace();
}
//STEP 3: 创建Statement
stmt = conn.prepareStatement(sql);
//stmt.setString(1,col);
stmt.setObject(1,param_value);
//STEP 4: 执行SQL语句
ResultSet rs = stmt.executeQuery(sql);
int col = rs.getMetaData().getColumnCount();
while(rs.next()){
for (int i = 1; i <= col; i++) {
System.out.print(rs.getString(i) + "\t");
if ((i == 2) && (rs.getString(i).length() < 8)) {
System.out.print("\t");
}
}
System.out.println("");
}
//STEP 5: 关闭JDBC连接
rs.close();
stmt.close();
conn.close();
}
}