jdbc_连接数据库
1、例一:
package com.vince.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class JdbcDemo{
public final static String URL = "jdbc:mysql://localhost:3306/test";
//用户名
public final static String USERNAME = "root";
//密码
public final static String PASSWORD = "admin";
public final static String DRIVER = "com.mysql.jdbc.Driver";
public static void main(String[] args) {
insert();
// update();
// delete();
}
public static void insert(){
try {
/*1、加载驱动*/
Class.forName(DRIVER);
/*2、连接数据库*/
Connection cnn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
//3、构造SQL语句
String sql = "insert into emp1 values(3,'小栏','女',9000,29,0,1);";
//4、构造一个Statement实例(用来发送SQL语句的载体)
Statement state= cnn.createStatement();
//5、执行SQL语句
state.executeUpdate(sql);
//6、关闭连接
state.close();
cnn.close();
System.out.println("success.");
} catch (ClassNotFoundException e) {
System.out.println("加载失败");
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
例二:
package com.vince.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JdbcDemo{
public final static String URL = "jdbc:mysql://localhost:3306/test";
//用户名
public final static String USERNAME = "root";
//密码
public final static String PASSWORD = "admin";
public final static String DRIVER = "com.mysql.jdbc.Driver";
public static void main(String[] args) {
// insert();
select();
// update();
// delete();
}
public static void insert(){
try {
/*1、加载驱动*/
Class.forName(DRIVER);
/*2、连接数据库*/
Connection cnn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
//3、构造SQL语句
String sql = "insert into emp1 values(3,'小栏','女',9000,29,0,1);";
//4、构造一个Statement实例(用来发送SQL语句的载体)
Statement state= cnn.createStatement();
//5、执行SQL语句
state.executeUpdate(sql);
//6、关闭连接
state.close();
cnn.close();
System.out.println("success.");
/*添删改都是一样的,把sql语句一改就可以了*/
} catch (ClassNotFoundException e) {
System.out.println("加载失败");
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void select(){
try {
Class.forName(DRIVER);
Connection conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
String sql = "select * from emp1 ";
Statement state = conn.createStatement();
//执行查询并返回结果集
ResultSet rs = state.executeQuery(sql);
while(rs.next()){
int id = rs.getInt(1);
String name=rs.getString(2);
String gender=rs.getString(3);
float age=rs.getFloat(4);
System.out.println(id+name+gender+age);
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
例三:使用PreparedStatement插入数据
package com.vince.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class PreparedStatement_demo {
//数据库连接地址
public final static String URL = "jdbc:mysql://localhost:3306/test"; //用户名
public final static String USERNAME = "root";
//密码
public final static String PASSWORD = "admin";
public final static String DRIVER = "com.mysql.jdbc.Driver";
/**
* 使用
* @param PreparedStatement插入数据
*/
public static void insert(Person p){
try {
Class.forName(DRIVER);
Connection conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
String sql = "insert into emp1(id,name,gendar,salary,age)values(?,?,?,?,?)";
PreparedStatement ps = conn.prepareStatement(sql);
//设置占位符对应的值
ps.setInt(1, p.getId());
ps.setString(2, p.getName());
ps.setString(3, p.getGendar());
ps.setFloat(4,p.getSalary());
ps.setFloat(5,p.getAge());
ps.executeUpdate();
ps.close();
conn.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Person p = new Person(7,"小黑","男",5000,30);
insert(p);
// update(p);
// delete(3);
// p = findById(2);
System.out.println(p);
}
}
-----------------------
同文件夹新建一个Person类
package com.vince.jdbc;
public class Person {
private int id;
private String name;
private String gendar;
private float salary;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGendar() {
return gendar;
}
public void setGendar(String gendar) {
this.gendar = gendar;
}
public float getSalary() {
return salary;
}
public void setSalary(float salary) {
this.salary = salary;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Person(int id, String name, String gendar, float salary ,int age) {
super();
this.id = id;
this.name = name;
this.gendar = gendar;
this.salary = salary;
this.age= age;
}
}