java操作mysql的小程序

来源:互联网 发布:淘宝秋季女装连衣裙 编辑:程序博客网 时间:2024/06/04 00:32
package jdbc;
import java.sql.*;


public class Mysql{ 
public static void main(String args[])

try 
{
Class.forName("com.mysql.jdbc.Driver");
//加载MYSQL JDBC驱动程序 
System.out.println("Success loading Mysql Driver!");

catch (Exception e) 

System.out.print("Error loading Mysql Driver!"); e.printStackTrace(); 
}

try 

Connection connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/XXXXdb","root","password"); 
System.out.println("Success connect Mysql server!");

//select code 
Statement stmt = connect.createStatement(); 
ResultSet rs = stmt.executeQuery("select * from am_api");
//am_api为表的名称  
while (rs.next()) 

System.out.println(rs.getString("API_NAME"));
//API_NAME是表am_api的一个字段
}

catch (Exception e) 

System.out.print("get data error!"); 
e.printStackTrace(); 

}
}
原创粉丝点击