servlet 中 java 访问 Mysql 的代码示例

Connection conn = null;
String url="",QueryStr="";
Statement stmt = null;
ResultSet rs = null;

String MySQL_IP = "192.180.15.1";
String MySQL_ID = "mht";
String MySQL_PW = "19951101";
try{
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
// Class.forName("com.mysql.jdbc.Driver").newInstance();//使用这一行与上一行问题一样
url ="jdbc:mysql://" MySQL_IP"/basic?user=" MySQL_ID"&password=" MySQL_PW"&useUnicode=true&characterEncoding=gb2312";
conn = DriverManager.getConnection(url);
stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
}catch(Exception e){
e.printStackTrace();
out.println(e);
}
String str="";
QueryStr="select * from station where TYPE='0' or TYPE='1' or TYPE='2'";
rs=stmt.executeQuery(QueryStr);
while(rs.next()){
str=rs.getString("STATION");
out.print(str);
}
if(rs != null) rs.close();
if(stmt != null) stmt.close();
if(conn != null) conn.close();

你可能感兴趣的:(servlet)