jdbc select

时间:2023-03-10 03:07:41
jdbc select
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException; public class mainclass { private static Connection getConn() {
// String driver = "com.mysql.jdbc.Driver";
// String url = "jdbc:mysql://localhost:3306/samp_db";
String driver = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@192.168.1.200:1521:tyhzyl";
String username = "xnh";
String password = "a123456";
Connection conn = null;
try {
Class.forName(driver); // classLoader,加载对应驱动
conn = (Connection) DriverManager.getConnection(url, username,
password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
} private static Integer getAll() {
Connection conn = getConn();
String sql = "select * from ZD_JB where f_jbid=?";
PreparedStatement pstmt;
try {
pstmt = (PreparedStatement) conn.prepareStatement(sql);
pstmt.setString(1, "Z35.001");
ResultSet rs = pstmt.executeQuery();
int col = rs.getMetaData().getColumnCount();
System.out.println("============================");
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("");
}
System.out.println("============================");
} catch (SQLException e) {
e.printStackTrace();
}
return null;
} public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("hello jdbc."); getAll();
} }