1 源代码
2 MySQL驱动
package cn.itcast.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Base {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
test();
}
static void test() throws SQLException, ClassNotFoundException {
// 1.注册驱动(一共三种注册驱动的方式)
//DriverManager.registerDriver(new com.mysql.jdbc.Driver());
//System.setProperty("jdbc.drivers", "com.mysql.jdbc.Driver");
Class.forName("com.mysql.jdbc.Driver");// 推荐方式
// 2.建立连接
String url = "jdbc:mysql://10.105.43.220:3306/jdbc";
String user = "sa";
String password = "123456";
Connection conn = DriverManager.getConnection(url, user, password);
// 3.创建语句
Statement st = conn.createStatement();
// 4.执行语句
ResultSet rs = st.executeQuery("select * from user");
// 5.处理结果
while (rs.next()) {
System.out.println(rs.getObject(1) + "\t" + rs.getObject(2) + "\t"
+ rs.getObject(3) + "\t" + rs.getObject(4));
}
// 6.释放资源
rs.close();
st.close();
conn.close();
}
}
2 MySQL驱动