📄 testmysql.java
字号:
package org.perfect.struts.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class TestMysql {
private static final String driverClass = "com.mysql.jdbc.Driver";
private static final String url ="jdbc:mysql://localhost/j2ee";
private static final String user = "root";
private static final String password = "root";
private static Connection conn = null;
private static PreparedStatement pstmt = null;
private static ResultSet rs = null;
public static void main(String[] args){
try {
Class.forName(driverClass);
conn = DriverManager.getConnection(url, user, password);
pstmt = conn.prepareStatement("select * from user");
rs = pstmt.executeQuery();
while(rs.next()){
String name = rs.getString(1);
String password = rs.getString(2);
System.out.println(name+" "+password);
}
} catch (Exception e) {
e.printStackTrace();
}
finally{
if(rs != null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(pstmt != null){
try {
pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -