Idea的Javaweb开发 1. 用java语言调用数据库; 前提:idea配置好jar包
字符类型:GDK UTF8等等。 增删改:
import java.sql.*;
public class mysqlconnections {
public static void main(String[] args) throws Exception { //导入驱动包 Class.forName(\);
//链接至数据库 String jdbc = \;//加上了字符编码的方式 Connection conn = DriverManager.getConnection(jdbc, \, \);
Statement state = conn.createStatement();//容器 // String sql =\李六','男','2000-10-02','......')\;增
加
// String sql =\;//删除
// String sql=\钟霓' where id='6' \;//修改
state.executeUpdate(sql); //将sql语句上传至数据库执行
conn.close();
} }
查找:
import java.sql.*;
public class mysqlconnections {
public static void main(String[] args) throws Exception {
Class.forName(\);//导入数据包 String jdbc =\; Connection conn = DriverManager.getConnection(jdbc,\,\); Statement state =conn.createStatement(); String sql =\* from student\; ResultSet one = state.executeQuery(sql); while(one.next()){
System.out.println(one.getString(1)+\\+one.getString(2)+\\+one.getString(3)+\\+one.getString(4)+\\+one.getString(5)); }
conn.close(); } }
输入账号和密码,在数据库中获取,进而登入或者显示错误
import java.sql.*; import java.util.*;
public class mysqlconnections {
public static void main(String[] args) throws Exception { Scanner scanf = new Scanner(System.in);
System.out.println(\请输入账号\); String name = scanf.nextLine(); System.out.println(\请输入密码\); String password = scanf.nextLine();
Class.forName(\);//导入数据包 String jdbc = \;//characterEncoding=UTF8 Connection conn = DriverManager.getConnection(jdbc, \, \);
Statement state = conn.createStatement();
String sql = \* from pass1 where name='\+ name + \+ password + \; ResultSet re = state.executeQuery(sql);
if (re.next()) {
System.out.println(\成功登陆!\+ re.getString(1) + \欢迎你\); } else {
System.out.println(\账号或密码错误\); }
conn.close(); }
}