Java用JDBC连接mysql
可以用netstat -tulpn
查看centos下mysql运行的端口号,默认为3306
首先import(那些导jar包就不用说了吧…):
import java.sql.*;
创建class类用于连接
- 不安全的连接方式(sql注入)
public class JDBCDemoMySQL {
private static final String URL = "jdbc:mysql://mysql服务器地址:3306/数据库实例名";
private static final String USERNAME = "name";
private static final String PWD = "password";
/*********************************************************************************
输入insert/delete/update语句执行,建议用于update
**********************************************************************************/
public static void update(String sql) {
Connection connection = null;//mysql连接,通过驱动drivermanager产生
Statement stmt = null;//mysql语句执行类,通过Connection产生
ResultSet rs = null ;//mysql查询返回值,通过statement产生
try {
// a.导入驱动,加载具体的驱动类
Class.forName("com.mysql.jdbc.Driver");// 加载具体的驱动类
// b.与数据库建立连接
connection = DriverManager.getConnection(URL, USERNAME, PWD);
// c.发送sql,执行(增删改、查)
stmt = connection.createStatement();
int count = stmt.executeUpdate(sql); // 返回值表示 增删改 几条数据
// d.处理结果
if (count > 0) {
System.out.println("操作成功!");//提示操作成功
}
} catch (ClassNotFoundException e) {//抓取异常
e.printStackTrace();//输出异常
} catch (SQLException e) {
e.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
}
finally {
try {//关闭连接
if(stmt!=null) stmt.close();
if(connection!=null) connection.close();
} catch(SQLException e) {
e.printStackTrace();
}
}}
/**********************************************************************************
输入insert/delete/update语句执行,建议用于delete
***********************************************************************************/
public static void delete(String sql) {
Connection connection = null;//mysql连接,通过驱动drivermanager产生
Statement stmt = null;//mysql语句执行类,通过Connection产生
ResultSet rs = null ;//mysql查询返回值,通过statement产生
try {
// a.导入驱动,加载具体的驱动类
Class.forName("com.mysql.jdbc.Driver");// 加载具体的驱动类
// b.与数据库建立连接
connection = DriverManager.getConnection(URL, USERNAME, PWD);
// c.发送sql,执行(增删改、查)
stmt = connection.createStatement();
int count = stmt.executeUpdate(sql); // 返回值表示 增删改 几条数据
// d.处理结果
if (count > 0) {
System.out.println("操作成功!");//提示操作成功
}
} catch (ClassNotFoundException e) {//抓取异常
e.printStackTrace();//输出异常
} catch (SQLException e) {
e.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
}
finally {
try {//关闭连接
if(stmt!=null) stmt.close();
if(connection!=null) connection.close();
} catch(SQLException e) {
e.printStackTrace();
}
}}
/*******************************************************************************
输入insert/delet/update语句执行,建议用于insert
********************************************************************************/
public static void insert(String sql) {
Connection connection = null;//mysql连接,通过驱动drivermanager产生
Statement stmt = null;//mysql语句执行类,通过Connection产生
ResultSet rs = null ;//mysql查询返回值,通过statement产生
try {
// a.导入驱动,加载具体的驱动类
Class.forName("com.mysql.jdbc.Driver");// 加载具体的驱动类
// b.与数据库建立连接
connection = DriverManager.getConnection(URL, USERNAME, PWD);
// c.发送sql,执行(增删改、查)
stmt = connection.createStatement();
int count = stmt.executeUpdate(sql); // 返回值表示 增删改 几条数据
// d.处理结果
if (count > 0) {
System.out.println("操作成功!");//提示操作成功
}
} catch (ClassNotFoundException e) {//抓取异常
e.printStackTrace();//输出异常
} catch (SQLException e) {
e.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
}
finally {//放这里关闭数据库连接,注意防止空指针异常
try {//关闭连接
if(stmt!=null) stmt.close();
if(connection!=null) connection.close();
} catch(SQLException e) {
e.printStackTrace();
}
}}
/*******************************************************************************
输入select语句执行
********************************************************************************/
public static void select(String sql) {
Connection connection = null;//mysql连接,通过驱动drivermanager产生
Statement stmt = null;//mysql语句执行类,通过Connection产生
ResultSet rs = null ;//mysql查询返回值,通过statement产生
try {
// a.导入驱动,加载具体的驱动类
Class.forName("com.mysql.jdbc.Driver");// 加载具体的驱动类
// b.与数据库建立连接
connection = DriverManager.getConnection(URL, USERNAME, PWD);
// c.发送sql,执行(增删改、查)
rs = stmt.executeQuery(sql);
// d.抓取结果
while(rs.next()){
int num = rs.getInt("column_name_int");//对应的列名必须是int类型
String str = rs.getString("column_name_string");//对应的列名必须是String类型
System.out.println(num+"--"+str);
}
} catch (ClassNotFoundException e) {//抓取异常
e.printStackTrace();//输出异常
} catch (SQLException e) {
e.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
}
finally {//放这里关闭数据库连接,注意防止空指针异常
try {//关闭连接
if(stmt!=null) stmt.close();
if(connection!=null) connection.close();
} catch(SQLException e) {
e.printStackTrace();
}
}
}
}
- 预编译方式防止sql注入
//有占位符'?'表示变量,暂时未知,需要用户输入,下面给出一个sql语句例子
String sql = "insert into student values(?,?,?,?)";
public class JDBCDemoMySQL {
private static final String URL = "jdbc:mysql://mysql服务器地址:3306/数据库实例名";
private static final String USERNAME = "name";
private static final String PWD = "password";
/*********************************************************************************
输入insert/delete/update语句执行,建议用于update
**********************************************************************************/
public static void update(String sql) {
Connection connection = null;//mysql连接,通过驱动drivermanager产生
PreparedStatement pstmt = null;//mysql语句执行类,通过Connection产生
ResultSet rs = null ;//mysql查询select返回值,通过statement产生
try {
// a.导入驱动,加载具体的驱动类
Class.forName("com.mysql.jdbc.Driver");// 加载具体的驱动类
// b.与数据库建立连接
connection = DriverManager.getConnection(URL, USERNAME, PWD);
// c.发送sql,执行(增删改、查)
//预编译,防止sql注入
//这里的setInt,setString参数第一个表示第几个占位符,第二个表示占位符带入的数据,注意要用对的函数带入!
pstmt = connection.prepareStatement(sql);//预编译
//占位符赋值
pstmt.setInt(1, 36);
pstmt.setString(2, "zhangsan");
pstmt.setInt(3, 56);
pstmt.setString(4, "s3");
int count = pstmt.executeUpdate(); // 返回值表示 增删改 几条数据
// d.处理结果
if (count > 0) {
System.out.println("操作成功!");//提示操作成功
}
} catch (ClassNotFoundException e) {//抓取异常
e.printStackTrace();//输出异常
} catch (SQLException e) {
e.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
}
finally {
try {//关闭连接
if(pstmt!=null) pstmt.close();
if(connection!=null) connection.close();
} catch(SQLException e) {
e.printStackTrace();
}
}}
/**********************************************************************************
输入insert/delete/update语句执行,建议用于delete
***********************************************************************************/
public static void delete(String sql) {
Connection connection = null;//mysql连接,通过驱动drivermanager产生
PreparedStatement pstmt = null;//mysql语句执行类,通过Connection产生
ResultSet rs = null ;//mysql查询select返回值,通过statement产生
try {
// a.导入驱动,加载具体的驱动类
Class.forName("com.mysql.jdbc.Driver");// 加载具体的驱动类
// b.与数据库建立连接
connection = DriverManager.getConnection(URL, USERNAME, PWD);
// c.发送sql,执行(增删改、查)
//预编译,防止sql注入
//这里的setInt,setString参数第一个表示第几个占位符,第二个表示占位符带入的数据,注意要用对的函数带入!
pstmt = connection.prepareStatement(sql);//预编译
//占位符赋值
pstmt.setInt(1, 36);
pstmt.setString(2, "zhangsan");
pstmt.setInt(3, 56);
pstmt.setString(4, "s3");
int count = pstmt.executeUpdate(); // 返回值表示 增删改 几条数据
// d.处理结果
if (count > 0) {
System.out.println("操作成功!");//提示操作成功
}
} catch (ClassNotFoundException e) {//抓取异常
e.printStackTrace();//输出异常
} catch (SQLException e) {
e.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
}
finally {
try {//关闭连接
if(pstmt!=null) pstmt.close();
if(connection!=null) connection.close();
} catch(SQLException e) {
e.printStackTrace();
}
}}
/*******************************************************************************
输入insert/delet/update语句执行,建议用于insert
********************************************************************************/
public static void insert(String sql) {
Connection connection = null;//mysql连接,通过驱动drivermanager产生
PreparedStatement pstmt = null;//mysql语句执行类,通过Connection产生
ResultSet rs = null ;//mysql查询select返回值,通过statement产生
try {
// a.导入驱动,加载具体的驱动类
Class.forName("com.mysql.jdbc.Driver");// 加载具体的驱动类
// b.与数据库建立连接
connection = DriverManager.getConnection(URL, USERNAME, PWD);
// c.发送sql,执行(增删改、查)
//预编译,防止sql注入
//这里的setInt,setString参数第一个表示第几个占位符,第二个表示占位符带入的数据,注意要用对的函数带入!
pstmt = connection.prepareStatement(sql);//预编译
//占位符赋值
pstmt.setInt(1, 36);
pstmt.setString(2, "zhangsan");
pstmt.setInt(3, 56);
pstmt.setString(4, "s3");
int count = pstmt.executeUpdate(); // 返回值表示 增删改 几条数据
// d.处理结果
if (count > 0) {
System.out.println("操作成功!");//提示操作成功
}
} catch (ClassNotFoundException e) {//抓取异常
e.printStackTrace();//输出异常
} catch (SQLException e) {
e.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
}
finally {//放这里关闭数据库连接,注意防止空指针异常
try {//关闭连接
if(pstmt!=null) pstmt.close();
if(connection!=null) connection.close();
} catch(SQLException e) {
e.printStackTrace();
}
}}
/*******************************************************************************
输入select语句执行
********************************************************************************/
public static void select(String sql) {
Connection connection = null;//mysql连接,通过驱动drivermanager产生
PreparedStatement pstmt = null;//mysql语句执行类,通过Connection产生
ResultSet rs = null ;//mysql查询select返回值,通过statement产生
try {
// a.导入驱动,加载具体的驱动类
Class.forName("com.mysql.jdbc.Driver");// 加载具体的驱动类
// b.与数据库建立连接
connection = DriverManager.getConnection(URL, USERNAME, PWD);
// c.发送sql,执行(增删改、查)
//预编译,防止sql注入
//这里的setInt,setString参数第一个表示第几个占位符,第二个表示占位符带入的数据,注意要用对的函数带入!
pstmt = connection.prepareStatement(sql);//预编译
//占位符赋值
pstmt.setInt(1, 36);
pstmt.setString(2, "zhangsan");
pstmt.setInt(3, 56);
pstmt.setString(4, "s3");
rs = pstmt.executeQuery();
// d.抓取结果
while(rs.next()){
int num = rs.getInt("column_name_int");//对应的列名必须是int类型
String str = rs.getString("column_name_string");//对应的列名必须是String类型
System.out.println(num+"--"+str);
}
} catch (ClassNotFoundException e) {//抓取异常
e.printStackTrace();//输出异常
} catch (SQLException e) {
e.printStackTrace();
} catch(Exception e) {
e.printStackTrace();
}
finally {//放这里关闭数据库连接,注意防止空指针异常
try {//关闭连接
if(pstmt!=null) pstmt.close();
if(connection!=null) connection.close();
} catch(SQLException e) {
e.printStackTrace();
}
}
}
}