连接mysql数据库相关语句
2011-07-28 11:29
246 查看
package com.test.database;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Logger;
public class DBConnect {
static Connection conn = null;
static Statement st = null;
static ResultSet rs = null;
public static Connection getConnection() throws SQLException {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
Logger.getLogger(e.getMessage());
}
try {
conn = DriverManager.getConnection(
"Jdbc:mysql://localhost:3306/test", "root", "root");
} catch (SQLException e) {
e.getMessage();
if (conn != null) {
conn.close();
}
}
return conn;
}
public static int getInsert(int sid, String sname, String sex)
throws SQLException {
conn = getConnection();
int i = 0;
String sql = "insert into student values("+sid+",'" + sname + "','" + sex
+ "')";
/*
* 这里要注意细节上的错误,如果把sid写成字符串,在插入数据的时候会出现异常。
而且在这个地方通常网上说的那种情况“数据库里序号没有设置成自增”在这里不适用。
*/
try {
st = conn.createStatement();
i = st.executeUpdate(sql);
} catch (Exception e) {
if (conn != null)
conn.close();
e.printStackTrace();
}
return i;
}
public static String getSelect() throws SQLException {
conn = getConnection();
String sql = "select * from student";
try {
st = conn.createStatement();
rs = st.executeQuery(sql);
while (rs.next()) {
rs.getInt("sid");
rs.getString("sname");
rs.getString("sex");
System.out.println(rs.getInt("sid") + ","
+ rs.getString("sname") + "," + rs.getString("sex"));
}
} catch (SQLException e) {
rs.close();
if (conn != null)
conn.close();
e.printStackTrace();
}
return null;
}
public static int getUpdate(int sid, String sna, String se)
throws SQLException {
conn = getConnection();
int i = 0;
String sql = "update student set sname='" + sna + "',sex='" + se + "'";
sql = sql + " where sid=" + sid;
try {
st = conn.createStatement();
i = st.executeUpdate(sql);
} catch (Exception e) {
if (conn != null)
conn.close();
e.printStackTrace();
}
return i;
}
public static void getDelete(int sid) throws Exception {
conn = getConnection();
String sql = "delete from student where sid=" + sid;
conn.createStatement().execute(sql);
if (conn != null)
conn.close();
}
public static void main(String args[]) throws Exception {
DBConnect.getConnection();
// DBConnect.getInsert(6, "werrr", "m");
DBConnect.getUpdate(1, "1", "uii");
DBConnect.getSelect();
DBConnect.getDelete(0);
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Logger;
public class DBConnect {
static Connection conn = null;
static Statement st = null;
static ResultSet rs = null;
public static Connection getConnection() throws SQLException {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
Logger.getLogger(e.getMessage());
}
try {
conn = DriverManager.getConnection(
"Jdbc:mysql://localhost:3306/test", "root", "root");
} catch (SQLException e) {
e.getMessage();
if (conn != null) {
conn.close();
}
}
return conn;
}
public static int getInsert(int sid, String sname, String sex)
throws SQLException {
conn = getConnection();
int i = 0;
String sql = "insert into student values("+sid+",'" + sname + "','" + sex
+ "')";
/*
* 这里要注意细节上的错误,如果把sid写成字符串,在插入数据的时候会出现异常。
而且在这个地方通常网上说的那种情况“数据库里序号没有设置成自增”在这里不适用。
*/
try {
st = conn.createStatement();
i = st.executeUpdate(sql);
} catch (Exception e) {
if (conn != null)
conn.close();
e.printStackTrace();
}
return i;
}
public static String getSelect() throws SQLException {
conn = getConnection();
String sql = "select * from student";
try {
st = conn.createStatement();
rs = st.executeQuery(sql);
while (rs.next()) {
rs.getInt("sid");
rs.getString("sname");
rs.getString("sex");
System.out.println(rs.getInt("sid") + ","
+ rs.getString("sname") + "," + rs.getString("sex"));
}
} catch (SQLException e) {
rs.close();
if (conn != null)
conn.close();
e.printStackTrace();
}
return null;
}
public static int getUpdate(int sid, String sna, String se)
throws SQLException {
conn = getConnection();
int i = 0;
String sql = "update student set sname='" + sna + "',sex='" + se + "'";
sql = sql + " where sid=" + sid;
try {
st = conn.createStatement();
i = st.executeUpdate(sql);
} catch (Exception e) {
if (conn != null)
conn.close();
e.printStackTrace();
}
return i;
}
public static void getDelete(int sid) throws Exception {
conn = getConnection();
String sql = "delete from student where sid=" + sid;
conn.createStatement().execute(sql);
if (conn != null)
conn.close();
}
public static void main(String args[]) throws Exception {
DBConnect.getConnection();
// DBConnect.getInsert(6, "werrr", "m");
DBConnect.getUpdate(1, "1", "uii");
DBConnect.getSelect();
DBConnect.getDelete(0);
}
}
相关文章推荐
- 查看mysql数据库连接数、并发数相关信息
- MySQL数据库中like语句及相关优化器tips【转】
- java连接mysql数据库相关代码
- mysql数据库中相关sql语句
- navicat连接阿里云mysql数据库服务器遇到的1130等相关问题
- 一个简单的连接mysql数据库的语句
- php操作mysql数据库的连接语句以及最简单的增删改查语句
- PHP连接mysql数据库基本语句
- PHP中使用PDO连接MySQL数据库查询相关内容
- 查看/修改mysql数据库连接数、并发数相关信息
- Python连接MySQL数据库执行sql语句时的参数问题
- vs2013下C/C++连接mysql数据库相关问题
- 查看mysql数据库连接数、并发数相关信息
- Java 建立mysql数据库连接的语句
- C# 连接MySQL数据库的连接语句
- SpringBoot中连接MYSQL数据库,并使用JPA进行数据库的相关操作
- 6.(Mysql数据管理相关)连接MYSQL,修改密码,增加新用户,数据库相关命令,表操作相关命令,数据相关命令,数据库sql导入和导出,备份数据库,查看不到mysql数据库的解决办法
- 查看mysql数据库连接数、并发数相关信息
- SpringBoot中连接MYSQL数据库,并使用JPA进行数据库的相关操作
- .net连接mysql数据库语句