import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class ConnectMySQL {
/**
* @param args
* @throws ClassNotFoundException
*/
public static void main(String[] args) throws ClassNotFoundException {
//JDBC��һЩ����
String URL = "jdbc:mysql://localhost:3306/zhang";
String driverName = "com.mysql.jdbc.Driver";
String sql = "select * from student";
String userName = "root";//root
String password = "kfc123456";//123456
Connection conn = null;
Class.forName(driverName);//jdbc4.0 ������ʹ������������ע�����
try {
conn = DriverManager.getConnection(URL,userName,password);
Statement statement = conn.createStatement();
ResultSet resultSet = statement.executeQuery(sql);
// id | name
while(resultSet.next()){
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
System.out.println("id="+id+" name="+name);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
if(conn!=null)
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}//����ʹ��Try..with..resources���
conn = null;
}
}
public static void insert(Statement statement){
String sql = "insert into student value(5,jinlei)";
try {
statement.execute(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void delete(Statement statement){
String sql = "delete from student where id = 50";
try {
statement.execute(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
一键复制
编辑
Web IDE
原始数据
按行查看
历史