JAVA连接到MYSQL数据库

首先要下载驱动,
import java.sql.*;
public class ConnecttoMysql
{
private final String driver = "com.mysql.jdbc.Driver";
private final String url = "jdbc:mysql://localhost:3306/information_schema";
private final String user = "root";
private final String password = "261210";
private Connection conn = null;
private Statement stmt = null;
private ResultSet rs = null;
public ConnecttoMysql()
{
try
{
Class.forName(driver);
conn = DriverManager.getConnection(url,user,password);
System.out.println("连接成功了");
}
catch (Exception e)
{
System.out.println("加载驱动失败......");
}
try
{
stmt = conn.createStatement();
rs= stmt.executeQuery("select PRIVILEGE_TYPE from USER_PRIVILEGES");
System.out.println("提取数据成功!");
}
catch (Exception e)
{
System.out.println("提取数据失败......");
}
try
{
while(rs.next())
{
String privilege_type =rs.getString(1);
System.out.println(privilege_type);
}
}
catch(Exception e){}
}
public void close()
{
try
{
rs.close();
stmt.close();
conn.close();
}
catch (Exception e)
{
System.out.println("数据库连接关闭失败......");
}
}
public static void main(String args[])
{
new ConnecttoMysql();
}
}
Tags: 

延伸阅读

最新评论

发表评论