今天在学习kauklahti的时候,自己写了一个Jdbc连接mysql的例子。算是复习了一下Jdbc的用法。
//------------------------------------------------------------------------------------ import java.sql.Connection; public class Database{ private Connection connection; public Database(){ Class.forName("com.mysql.jdbc.Driver").newInstance(); // 注册驱动 } // 查询help_relation中help_topic_id小于100的数据, // 并打印输出 public excuteQuery(){ String url ="jdbc:mysql://localhost:3306/mysql?user=root&password=654321"; connection = DriverManeger.getConnection(url); Statement statement = connection.createStatement(); String sql ="select * from help_relation where help_topic_id<100"; ResultSet rs = statement.executeQuery(); while(rs.next){ System.out.printlnl(rs.getString(1)); } } // 测试主函数 public static void main(String[] args){ new Database().executeQuery(); } } //-----------------------------------------------------------------------------------