求Java 注册登录 连接到数据库的源代码。

2024-11-01 08:42:11
有4个网友回答
网友(1):

package cc.icoc.javaxu.dao;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;public class MySQLOprea { /** * 增加记录 INSERT INTO 表名(字段名,字段名) VALUES (值,值); * 删除记录 DELETE FROM 表名 WHERE 条件() * 修改记录 UPDATE 表名 SET 字段=值,字段=值 WHERE 条件 * 查询记录 SELECT 字段,字段 FROM 表名 WHERE 条件 */ ResultSet rs = null; Connection conn = null; Statement statement = null; //链接 public Connection connSQL() { String DRIVER = "com.mysql.jdbc.Driver";// 数据库驱动 String URL = "jdbc:mysql://localhost:3306/mydata?useUnicode=true&characterEncoding=gb2312";// String DBNAME = "root";// 用户名 String DBPASS = "341341";// 密码 try { Class.forName(DRIVER).newInstance();// 注册驱动 conn = DriverManager.getConnection(URL, DBNAME, DBPASS); statement = conn.createStatement(); } catch (Exception e) {} return conn; } //增 /** * 插入新记录的操作 * @param table 表名 * @param userName 插入的用户名 * @param passWord 插入的用户密码 * @return true代表插入成功,false代表插入失败 */ public String insert(String table, String userName, String passWord) { connSQL(); String s = "注册成功"; try { String insert = "insert into "+table+"(userName,passWord) values ("+"'"+userName+"'"+","+"'"+passWord+"'"+")"; statement.executeUpdate(insert); closeDB(); } catch (Exception e) { // TODO: handle exception s = "注册失败"+e.toString(); } return s; } //删 public void delete(String table, String whereValue) throws SQLException { String delete = "Delete from "+table+" where userName = "+whereValue; statement.executeUpdate(delete); } //改 public void update(String table, String whereValue , String newValue) throws SQLException { String update = "Update "+table+" set passWord ="+newValue+" where userName ="+whereValue; statement.executeUpdate(update); } //查 public String query(String table , String whereValue1 ,String whereValue2, String whatCol1, String whatCol2) throws SQLException { connSQL(); String query = null;// ResultSet set= null; try { query = "select "+whatCol1+","+whatCol2+" from "+table +" where "+whatCol1+"="+'"'+whereValue1+'"'+" and "+whatCol2+"="+'"'+whereValue2+'"'; rs = statement.executeQuery(query); closeDB(); } catch (Exception e) { // TODO: handle exception return "false exception:"+e.toString(); } if(rs.next()) { return "true:"; } return "false:"; } private void closeDB() { // TODO Auto-generated method stub try { if(rs != null) { rs.close(); } if(statement != null) { statement.close(); } if(conn != null) { conn.close(); } } catch (Exception e) { // TODO: handle exception System.out.println("数据库关闭时出现异常"); } }}

网友(2):

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public final class JdbcUtils {
private static String url = "sun.jdbc.odbc.JdbcOdbcDriver";//我这是连ODBC数据库

private JdbcUtils() {
}

static {
try {
Class.forName(url);
} catch (Exception e) {
throw new ExceptionInInitializerError(e);
}
}

public static Connection getConnection() throws SQLException {
return DriverManager.getConnection("jdbc:odbc:my","","");//根据你自己本地的数据库修改
}

public static void free(ResultSet rs, Statement st, Connection conn) {
try {
if (rs != null)
rs.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (st != null)
st.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (conn != null)
try {
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}

网友(3):

不就是一个查询语句。。select

网友(4):

留个邮箱或者qq我把我刚做作业的login文件传给你好了