package demo;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JdbcMySQL {
public static void main(String[] args) {
Connection conn = null;
Statement st = null;
ResultSet rs = null;
try {
// 连接MYSQL
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager
.getConnection(
"jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8",
"root", "mysql");
// 3.获取指令对象
st = conn.createStatement();
// 4.发送指令,并返回结果
rs = st.executeQuery("select * from userinfo");
// 5.处理结果
while (rs.next()) {
System.out.print("USERID:" + rs.getInt(1));
System.out.print("USERNAME:" + rs.getString(2));
System.out.print("USERPWD:" + rs.getString(3));
System.out.println();
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// 关闭对象
try {
if (rs != null) {
rs.close();
}
if (st != null) {
st.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}