连接数据库过程,并遍历数据库
import java.sql.*;
public class TestJDBC {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");// 1注册driver
conn = DriverManager
.getConnection("jdbc:mysql://localhost/mydata?user=root&password=root");// 2通过drivermanager.getconnection()拿到connection
stmt = conn.createStatement();// 3创建statement对象通过statement执行sql语句
rs = stmt.executeQuery("select * from dept");// 4拿到Resultset
while (rs.next()) {
System.out.print(rs.getString("deptno"));
System.out.println(rs.getString("dname"));// 5对ResultSet进行遍历
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
System.out.println("SQLException: " + e.getMessage());
System.out.println("SQLState: " + e.getSQLState());
System.out.println("VendorError: " + e.getErrorCode());
} finally {
try {
if (rs != null) {
rs.close();// 6关掉打开的资源
rs = null;
}
if (stmt != null) {
stmt.close();
stmt = null;
}
if (conn != null) {
conn.close();// 关掉连接,后打开的先关
conn = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
向数据库内插入一条记录
import java.sql.*;
public class TestDML {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try {
Class.forName("com.mysql.jdbc.Driver");// 1注册driver
conn = DriverManager
.getConnection("jdbc:mysql://localhost/mydata?user=root&password=root");// 2通过drivermanager.getconnection()拿到connection
stmt = conn.createStatement();// 3创建statement对象通过statement执行sql语句
String sql ="insert into dept2 values (007,'zmb','USA') ";//sql语句
stmt.executeUpdate(sql);//执行sql语句
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
System.out.println("SQLException: " + e.getMessage());
System.out.println("SQLState: " + e.getSQLState());
System.out.println("VendorError: " + e.getErrorCode());
} finally {
try {
if (stmt != null) {
stmt.close();
stmt = null;
}
if (conn != null) {
conn.close();// 关掉连接,后打开的先关
conn = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
通过输入向数据库插入数据
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class TestDML2 {
public static void main(String[] args) {
int deptno = 0;
String dname = null;
String loc = null;
Connection conn = null;
Statement stmt = null;
try {
deptno = Integer.parseInt(args[0]);
} catch (NumberFormatException e) {
System.out.println("参数错误,参数是数字型,请输入真确的参数!");
System.exit(-1);
}
try {
Class.forName("com.mysql.jdbc.Driver");// 1注册driver
conn = DriverManager
.getConnection("jdbc:mysql://localhost/mydata?user=root&password=root");// 2通过drivermanager.getconnection()拿到connection
stmt = conn.createStatement();// 3创建statement对象通过statement执行sql语句
String sql = "insert into dept2 values ("+deptno + ",'"+dname +",'"+ loc + "')" ;// sql语句
stmt.executeUpdate(sql);// 执行sql语句
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
System.out.println("SQLException: " + e.getMessage());
System.out.println("SQLState: " + e.getSQLState());
System.out.println("VendorError: " + e.getErrorCode());
} finally {
try {
if (stmt != null) {
stmt.close();
stmt = null;
}
if (conn != null) {
conn.close();// 关掉连接,后打开的先关
conn = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}