数据库操作:java连接MYSQL实例代码演示
package com.qiaole.conn;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Mysql {
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
String url = "jdbc:mysql://localhost:3306/test";
Connection conn = DriverManager
.getConnection(url, "root", "qiaole");
String sql = "select * from T_Person";
PreparedStatement psStatement = conn.prepareStatement(sql);
ResultSet rSet = psStatement.executeQuery();
System.out.println("\tMysql查询数据");
while (rSet.next()) {
System.out.println(rSet.getString(1) + "\t" + rSet.getInt(2)
+ "\t" + rSet.getString(3));
}
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
需要添加的包: