importjava.sql.*;publicclassJDBCExample1{publicstaticvoidmain(String[]args){StringconnectionURL="jdbc:mysql://localhost:3306/test?user=root&password?characterEncoding=GBK...
import java.sql.*;
public class JDBCExample1 {
public static void main(String[] args) {
String connectionURL =
"jdbc:mysql://localhost:3306/test?user=root&password?characterEncoding=GBK";
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(connectionURL);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select id,msg from echo_message");
while (rs.next()) {
System.out.println(rs.getString("id") + "\n" +
rs.getString("msg"));
}
}catch(SQLException e)
{
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
finally{
conn.close();
}
}
}
展开