###2017-6-21 专业实习第三天
1.关于JDBC的使用
- 1.先下载JDBC的驱动,官网里都会有下载的,在myeclipse的项目中新建文件夹lib,再讲jar文件复制过去,右键build path-add…最后会在Referenced…文件夹中出现如图一样的
- 2.在项目中加载驱动
一般单建个类
Class.forName(“com.mysql.jdbc.Driver”);这是需要try/catch异常,可以打印加载驱动是否成功的信息
public class JDBCTest {
public static void main(String[] args) {
//加载驱动
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("加载成功!");
catch (ClassNotFoundException e) {
System.out.println("加载驱动失败了!");
e.printStackTrace();
}
}
}
- 3.建立连接
//建立连接 后面是数据库的url 用户名 密码
Connection connection= DriverManager.getConnection("jdbc:mysql://localhost/mydb", "root","root");
- 4.创建sql 语句对象
//创建sql语句对象
Statement statement=connection.createStatement();
- 5.String 需要执行的语句
//要执行的sql语句
String sql="select *from student";
- 6.执行sql
//执行sql
ResultSet resultSetset=statement.executeQuery(sql);
ArrayList<Student> list=new ArrayList<Student>();
while (resultSetset.next()) {
System.out.println(resultSetset.getString("name"));
Student s=new Student();
s.setName(resultSetset.getString(1));
s.setAge(resultSetset.getInt(2));
s.setGender(resultSetset.getString("gender"));
s.setClassname(resultSetset.getNString("classname"));
s.setNumber(resultSetset.getInt("number"));
list.add(s);
}
System.out.println(list);
全部代码如下
package com.lanou.cmq;
import java.sql.*;
import java.util.ArrayList;
//网上下载jdbc
public class JDBCTest {
public static void main(String[] args) {
//加载驱动
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("加载成功!");
//建立连接
Connection connection= DriverManager.getConnection("jdbc:mysql://localhost/mydb", "root","root");//2
//创建sql语句对象
Statement statement=connection.createStatement();
//要执行的sql语句
String sql="select *from student";
//执行sql
ResultSet resultSetset=statement.executeQuery(sql);
//创建一个ArrayList <Student>对象list 来存放数据
ArrayList<Student> list=new ArrayList<Student>();
while (resultSetset.next()) {
System.out.println(resultSetset.getString("name"));
Student s=new Student();
s.setName(resultSetset.getString(1));
s.setAge(resultSetset.getInt(2));
s.setGender(resultSetset.getString("gender"));
s.setClassname(resultSetset.getNString("classname"));
s.setNumber(resultSetset.getInt("number"));
list.add(s);//将数据添加到list中
}
System.out.println(list);
} catch (ClassNotFoundException e) {
System.out.println("加载驱动失败了!");
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.lanou.cmq;
//建一个学生类,可以一张表建立一个类,比较简洁易懂哈
public class Student {
private String name;
private int age;
private String gender;
private String classname;
private int number;
//下面的这些方法都是不用手打的 右键source get... set..
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getClassname() {
return classname;
}
public void setClassname(String classname) {
this.classname = classname;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
}
具体可以参考:
http://blog.csdn.net/tanyunlong_nice/article/details/40743637
后记:之前自己学JDBC时就懵懵的,,现在就想仔仔细细记下来,这样就更容易能理解学习些