1. 连接数据库,得到数据库连接变量
[java] view plaincopyprint?
-
注意连接数据库的时候
(1)打开DB Browser 新建一个Database Driver,注意添加Driver JARs的时候添加的包,我的是mysql-connector-java-5.0.3-bin.jar
(2)要将数据库jar包拷贝到工程下的WEB-INF\lib下
[java] view plaincopyprint?
- import java.sql.Connection;
- public class DBConnection
- {
- private String dbDriver="com.mysql.jdbc.Driver";
- private String dbUrl="jdbc:mysql://[ip地址]:[端口号]/[数据库名]";//根据实际情况变化
- private String dbUser="root";
- private String dbPass="root";
- public Connection getConn()
- {
- Connection conn=null;
- try
- {
- Class.forName(dbDriver);
- }
- catch (ClassNotFoundException e)
- {
- e.printStackTrace();
- }
- try
- {
- conn = DriverManager.getConnection(dbUrl,dbUser,dbPass);
- }
- catch (SQLException e)
- {
- e.printStackTrace();
- }
- return conn;
- }
- }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | import java.sql.Connection;
public class DBConnection
{
private String dbDriver= "com.mysql.jdbc.Driver" ;
private String dbUrl= "jdbc:mysql://[ip地址]:[端口号]/[数据库名]" ;//根据实际情况变化
private String dbUser= "root" ;
private String dbPass= "root" ;
public Connection getConn()
{
Connection conn=null;
try
{
Class.forName(dbDriver);
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
try
{
conn = DriverManager.getConnection(dbUrl,dbUser,dbPass);
}
catch (SQLException e)
{
e.printStackTrace();
}
return conn;
}
}
|
登入後複製
2. 插入操作
[java] view plaincopyprint?
- public int insert()
- {
- int i=0;
- String sql="insert into (表名)(列名1,列明2) values(?,?)";
- Connection cnn=getConn();
-
- try{
- PreparedStatement preStmt =cnn.prepareStement(sql);
- preStmt.setString(1,值);
- preStmt.setString(2,值);
- i=preStmt.executeUpdate();
- }
- catch (SQLException e)
- {
- e.printStackTrace();
- }
- return i;
- }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | public int insert()
{
int i=0;
String sql= "insert into (表名)(列名1,列明2) values(?,?)" ;
Connection cnn=getConn();
try {
PreparedStatement preStmt =cnn.prepareStement(sql);
preStmt.setString(1,值);
preStmt.setString(2,值);
i=preStmt.executeUpdate();
}
catch (SQLException e)
{
e.printStackTrace();
}
return i;
}
|
登入後複製
3. 更新操作
[java] view plaincopyprint?
- public int update
- {
- int i=0;
- String sql="update (表名) set (列名1)=?,列明2=? where (列名)=?";
- Connection cnn=getConn();
-
-
- try{
- PreparedStatement preStmt =cnn.prepareStatement(sql);
- preStmt.setString(1,(值));
- preStmt.setString(2,(值));
- preStmt.setInt(3,(值));
- i=preStmt.executeUpdate();
- }
- catch (SQLException e)
- {
- e.printStackTrace();
- }
- return i;
- }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | public int update
{
int i=0;
String sql= "update (表名) set (列名1)=?,列明2=? where (列名)=?" ;
Connection cnn=getConn();
try {
PreparedStatement preStmt =cnn.prepareStatement(sql);
preStmt.setString(1,(值));
preStmt.setString(2,(值));
preStmt.setInt(3,(值));
i=preStmt.executeUpdate();
}
catch (SQLException e)
{
e.printStackTrace();
}
return i;
}
|
登入後複製
4. 查找操作
[java] view plaincopyprint?
- public String select
- {
- String sql = "select * from (表名) where (列名)=(值)";
- Connection cnn = getConn();
- try
- {
- Statement stmt = conn.createStatement();
- ResultSet rs = stmt.executeQuery(sql);
-
- if(rs.next())
- {
- int m1 = rs.getInt(1);
- String m2 = rs.getString(2);
- }
-
- }
- catch (SQLException e)
- {
- e.printStackTrace();
- }
- return (相应的值的变量);
- }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | public String select
{
String sql = "select * from (表名) where (列名)=(值)" ;
Connection cnn = getConn();
try
{
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
if (rs.next())
{
int m1 = rs.getInt(1);
String m2 = rs.getString(2);
}
}
catch (SQLException e)
{
e.printStackTrace();
}
return (相应的值的变量);
}
|
登入後複製
5. 删除操作
[java] view plaincopyprint?
- public int delete()
- {
- String sql = "delete from (表名) where (列名)=(值)";
- int i=0;
- Connection conn = getConn();
- try
- {
- Statement stmt = conn.createStatement();
- i = stmt.executeUpdate(sql);
- }
- catch (SQLException e)
- {
- e.printStackTrace();
- }
- return i;
- }