package java_web;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
public class jdbcQueryDemo {
public final static String URL="jdbc:mysql://localhost:3306/test";
public final static String USERNAME="root";
public final static String PASSWORD="";
public final static String DRIVER="com.mysql.jdbc.Driver";
public static void query(){
try {
Class.forName(DRIVER);
Connection conn = (Connection) DriverManager.getConnection(URL,USERNAME,PASSWORD);
String sql = "select id,name,sex from test where id=3";
Statement state = (Statement) conn.createStatement();
ResultSet rs=state.executeQuery(sql);
while(rs.next()){
//rs.getInt("id");
int id=rs.getInt(1);
String name=rs.getString(2);
int sex=rs.getInt(3);
//String time=rs.getString("vtime");
System.out.println(id+"=="+name+"=="+sex+"==");
}
rs.close();
state.close();
conn.close();
} catch (ClassNotFoundException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
public static void main(String[] args) {
// TODO 自动生成的方法存根
query();
}
}
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
public class jdbcQueryDemo {
public final static String URL="jdbc:mysql://localhost:3306/test";
public final static String USERNAME="root";
public final static String PASSWORD="";
public final static String DRIVER="com.mysql.jdbc.Driver";
public static void query(){
try {
Class.forName(DRIVER);
Connection conn = (Connection) DriverManager.getConnection(URL,USERNAME,PASSWORD);
String sql = "select id,name,sex from test where id=3";
Statement state = (Statement) conn.createStatement();
ResultSet rs=state.executeQuery(sql);
while(rs.next()){
//rs.getInt("id");
int id=rs.getInt(1);
String name=rs.getString(2);
int sex=rs.getInt(3);
//String time=rs.getString("vtime");
System.out.println(id+"=="+name+"=="+sex+"==");
}
rs.close();
state.close();
conn.close();
} catch (ClassNotFoundException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
public static void main(String[] args) {
// TODO 自动生成的方法存根
query();
}
}